So both classes are in the folder. How am I able to call variables in one class from another another class. For example, I ahve 2 classes, Class1, and Class2. So in Class1, I have "double a" and its value. How can i refer to this "double a" and its value in Class2? What code do I have to write?

beginner in Java.

Recommended Answers

All 7 Replies

add a getA() method in Class1 that returns the value of a. Use reference of a Class1 object (whose "a" u need to access) and call getA() which will return the value of "a".

public Double getA()
{
   return a;
}

Depends on how you define your class variable. If you define it as "private" then you must provide a method to access the variable.

class ClassA {
  private double a;
  ...
  // must provide the access methods
  public double getA() { return a; }  // need this and look at the return type (should match)
  public void setA(double inA) { a = inA; }  // optional method
  ...
}

public ClassB {
  ...
  // create class A in this class
  ClassA aVar;
  ...  // initial the variable and do stuff
  // access the value
  double aValue = aVar.getA();
}

Now if you do not declare the modifier or declare it as public, then anyone can directly access it.

class ClassA {
  double a; // or you could do 'public double a;' too
  ...
}

public ClassB {
  ...
  // create class A in this class
  ClassA aVar;
  ...  // initial the variable and do stuff
  // access the value
  double aValue = aVar.a;
}

Id like to point out that declaring "ANY" data item as public violates Encapsulation laws... If you're not a hard-core OOPS guy, this aint a big deal... :)

It's not so much that it "violates encapsulation laws", it's more that it creates design problems and bugs that are really hard to fix. Declaring a constant as public is safe, but declaring a variable public means that anyone, anywhere, executing in any thread, may change its value at any time. You can code

public int a;
...
a= 1;
System.out.println(a);

and you have no guarantee that it will print the value 1, because some other code somewhere may have access and changed a's value.
Even if your code is single-threaded, you still have to worry about side-effects and consequences of changing a value. When some particular value is changed it may be necessary to update other values to match, or you may need to store that new value in a database. If you allow anyone to change the value you have no chance to deal with these.

ps, but yes, I am a hard-core OOPS guy, so I'm going to go with private variables and accessor methods every time.

Well, the OP asks how to access the variable, so I showed him/her how to access. :) I am not teaching him/her about what to do. :P

Yes, absolutely. No criticism of your post in any way intended. I was just adding my own take on that subject.

Yes, absolutely. No criticism of your post in any way intended. I was just adding my own take on that subject.

Me neither... :) Just adding my opinion about ur solution. Because Im Love oops and wud luv to follow its principles i wanted to post that piece of info here!
Im not belittling ur comment in any way whatsoever!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.