• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

member variables

 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this program
package untitled7;

public class test {
int das;// this das
public test() {
}

public static void main(String[] args) {
test test1 = new test();
int das=7;
System.out.println(??);
}
}

i want to print the value of the first das in the println statement not the second das
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have created one instance of the class test which is referenced by the variable test1. To access test1's variable das, you can use test1.das in your println statement.
 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats one way there is another way to reference the member variable???/
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, no. Copies of "das" occur only inside instances of the class. Without creating an instance, you don't have any member variables.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some more advanced techniques using reflection, but they don't add any advantage.

A object-oriented purist would prefer code that centralizes all direct references to das to one accessor method and one mutator method. This allows edits to be added in one place and lets you change the real das to, say, different units of measure without looking at every bit of code that depends on das. It also works better with JavaBeans.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic