How would I go about accessing data in one java class from another one? For example, I declare an integer with a value in class_one.java and want to make use of that data in class_two.java.
-
I know this question is very simple, but I'm trying to learn and I often don't know where to start when I get stuck like this. – THE DOCTOR Jan 24 '13 at 19:56
-
3You need to go through some good Java Tutorial. [Oracle tutorial](http://docs.oracle.com/javase/tutorial/) would be a good starting point. – Rohit Jain Jan 24 '13 at 19:57
-
I realize that I have a lot to learn. However, that doesn't answer my question. – THE DOCTOR Jan 24 '13 at 19:59
-
1Yes, Mr Doctor. You have to learn a lot. So, do that! Search for java tutorial for beginners. Take your time and learn. Do not ask trivial question: they can cause damage to your reputation. – AlexR Jan 24 '13 at 20:02
-
I most certainly am, but in the meantime I wanted to find an answer so I can make a little more progress with what I'm working on. Do not be rude as that can cause damage to your reputation. – THE DOCTOR Jan 24 '13 at 20:07
7 Answers
Either declare the variable as public:
public class class_one {
public Integer myValue;
}
or create a public getter (preferred), such as:
public class class_one {
private Integer myValue;
public Integer getMyValue() {
return myValue;
}
}

- 11,769
- 6
- 39
- 43
Expose your variable via a getter method and call that method on an instance of the desired class.

- 1
- 1

- 52,720
- 19
- 113
- 137
Create an instance of the first class in the other class and simply use its variables, if declared public in the first class.
class Foo
{
public int a=10;
}
In the other class, do this
Foo a=new Foo();
int test = a.value;

- 3,773
- 7
- 35
- 47
a) public
:visible for all classes
b) protected
: visible in subclasses and classes in the same package
c) default
: visible for classes in the same package
This works for variables and method access.

- 9,961
- 6
- 38
- 49
One one is to build a "get" function which will return the desired value
such as
public int getA(){
return a;
}
Another easy solution would be to use static variables. Put the "static" keyword before a variable:
static int a=5;
and to access the variable you use the class:
Class_One.a;
this would use the variable as if it were inside the same class.

- 3,577
- 5
- 35
- 66
It depends how your variable is declared. Here is the Oracle tutorial:
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

- 15,408
- 6
- 37
- 48
You can access a variable of one java class in other java class using either of the following ways:
- Declaring the accessing variable as public and accessing it directly in other class. But You should avoid it as it leads to tight coupling.
- Declaring the accessing variable as private and accessing it via getter Method . It is the preferred way of accessing variables as it keeps the variable safe from accidentally changing in other class.
- Using
reflection
package .

- 12,976
- 2
- 27
- 38