2

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
THE DOCTOR
  • 4,399
  • 10
  • 43
  • 64
  • 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
  • 3
    You 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
  • 1
    Yes, 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 Answers7

3

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;
    }
  }
Mike Clark
  • 11,769
  • 6
  • 39
  • 43
1

Expose your variable via a getter method and call that method on an instance of the desired class.

Community
  • 1
  • 1
Andrew White
  • 52,720
  • 19
  • 113
  • 137
0

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;
Aakash Anuj
  • 3,773
  • 7
  • 35
  • 47
0

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.

MrSmith42
  • 9,961
  • 6
  • 38
  • 49
0

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.

aclowkay
  • 3,577
  • 5
  • 35
  • 66
0

It depends how your variable is declared. Here is the Oracle tutorial:

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

gaborsch
  • 15,408
  • 6
  • 37
  • 48
0

You can access a variable of one java class in other java class using either of the following ways:

  1. Declaring the accessing variable as public and accessing it directly in other class. But You should avoid it as it leads to tight coupling.
  2. 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.
  3. Using reflection package .
Vishal K
  • 12,976
  • 2
  • 27
  • 38