File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes member variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "member variables" Watch "member variables" New topic

member variables

Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
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;

i want to print the value of the first das in the println statement not the second das
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
thats one way there is another way to reference the member variable???/
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Well, no. Copies of "das" occur only inside instances of the class. Without creating an instance, you don't have any member variables.

[Jess in Action][AskingGoodQuestions]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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.

Mike Gershman
SCJP 1.4, SCWCD in process
I agree. Here's the link:
subject: member variables
It's not a secret anymore!