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?
-
What has this got to do with JavaScript? And what have you tried? (Hint: usually you'd add a "getter" method in one class to return the value of the variable, then call the method from the other class.)Jon Skeet– Jon Skeet2013-02-07 06:44:34 +00:00Commented Feb 7, 2013 at 6:44
-
1@PradeepSimha: Those are generally bad suggestions. Public variables break encapsulation, and you shouldn't just arbitrarily make a variable static - you need to understand when that's suitable.Jon Skeet– Jon Skeet2013-02-07 06:45:46 +00:00Commented Feb 7, 2013 at 6:45
-
1@PradeepSimha: So why even suggest making it public in the first place?Jon Skeet– Jon Skeet2013-02-07 06:47:32 +00:00Commented Feb 7, 2013 at 6:47
-
3@JonSkeet, don't you think there will be no scenario in world where you wouldn't use public variables? I gave him ways of doing it, if OP is asking this basic question means he is very beginner in Java.. and dumping all lots of best practices before even knowing basics is a very bad suggestionPradeep Simha– Pradeep Simha2013-02-07 06:51:45 +00:00Commented Feb 7, 2013 at 6:51
-
2@JonSkeet, I don't like to extend this discussion (different view points) but my main argument is if person doesn't even know how to access variable, first let him know you can access it via public variables, then after one or two tries he will automatically and practically come to know that public variables are bad. A good programmer should have experienced both good ways and bad ways of doing it, if he know bad ways he can better learn good ways. That's my view pointPradeep Simha– Pradeep Simha2013-02-07 07:05:16 +00:00Commented Feb 7, 2013 at 7:05
5 Answers
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.
2 Comments
Also read about access specifiers in Java it might help.
2 Comments
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:
- if the field is
public, any code can access it (this makespublicvariables kind of dangerous unless they are also declaredfinal) - if the field is
protected, only code in the same package or in a subclass ofAcan access it - if the field has default access, only code in the same package as class A can access it
- if the field is
private, only code in classA(including inner classes ofA) 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
}
}
Comments
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 :)