Reputation: 41
I was wondering if it's possible to use a variable of a java class in another java class.Suppose variable Time is defined and calculated in Class A, how can I use it in Class B?
Upvotes: 0
Views: 482
Reputation: 37566
If you declare your Variable as public or static you will be able to access it from another class.
WHICH IS A VERY VERY BAD IDEA :)
Upvotes: 0
Reputation: 1499800
Other answers have suggested increasing a variable's visibility. Don't do this. It breaks encapsulation: the fact that your class uses a field to store a particular piece of information is an implementation detail; you should expose relevant information via the class's API (its methods) instead. You should make fields private in almost all cases.
Likewise, some other answers have suggested possibly making the variable static. Don't do this arbitrarily. You need to understand what static really means: it's saying that this piece of information is related to the type rather than to any one particular instance of the type. Occasionally that's appropriate, but it's generally a road towards less testable code - and in many cases it's clearly wrong. For example, a Person
class may well have a name
variable, but that certainly shouldn't be static - it's clearly a piece of information about a single person.
You should think carefully before exposing information anyway - consider whether there's a wider operation which the class in question could expose, instead of just giving away its data piecemeal - but when you do want to expose a field's value, use a property. For example:
public class Person {
private final String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
By exposing it via a method, you can later change the implementation details without breaking existing clients.
Then from another class, you'd just call the getName()
method:
// However you end up getting a reference to an instance of Person
Person person = ...;
String name = person.getName();
If you do have a static field, you can expose the value in the same way, but with a static method, which you'd call using the class name.
Be careful about returning values which are mutable, e.g. java.util.Date
. This is another reason for using a getter method instead of allowing direct access to the field - you can make the method return a defensive copy where you need to.
Upvotes: 7
Reputation: 234795
If the variable is static
, you can refer to it as A.Time
from any code that has access to the variable. There's only one Time
value for all of class A. If it is an instance variable, and you have an instance a
of class A
, you can refer to the variable as a.Time
. There's a separate value for each instance of class A
.
This is subject to Java's access rules:
public
, any code can access it (this makes public
variables kind of dangerous unless they are also declared final
)protected
, only code in the same package or in a subclass of A
can access itprivate
, only code in class A
(including inner classes of A
) can access it.Alternatively, you can provide an accessor method in class A:
public class A {
. . .
public class getTime() {
return this.Time; // the "this." is optional
}
}
Upvotes: 0
Reputation: 4393
If it is declared as public, you may use ClassA.yourVariable
. On the other hand, for private access modifier, include the getter to your ClassA
. On the ClassB
, call ClassA.getYourVariable()
.
Upvotes: 1