kush
kush

Reputation: 11

How can I access one class variable from another class?

There are two separate classes class A and class B. Both classes are in the same package. I want to access data of one class from the other class. How can I access variable data of a class?. My program is very lengthy, I just want the logic of this.

Class A.java

public class A
{ 
  public static void main(String ar[])  
  {
    int a=100;
  }  
}

Class B.java

public class B extends A
{
  public static void main(String m[])
  {
     A obj=new A();
     System.out.println("Variable of class A is :"+ obj.a);
  }
}

I have done this thing to get access like I declared a variable a as Static so that I can directly get access but it's not working. And when I am compiling B.java it is giving me error

cannot find symbol at := System.out.println("Variable of class A is :"+ obj.a);

And

Illegal start of expression

(when I am declaring variable a as public)

:-(error)public int a=100; [in class A].

Upvotes: 0

Views: 80

Answers (4)

xploreraj
xploreraj

Reputation: 4362

One-liner answer: To access a variable outside a class, make it class-level. You have written a method-level variable that's accessible only inside that scope (method).

To elaborate:

There are to separate classes class One and class Two. Both of classes are in same package. I want to access one class data into other class how can i access variable data.

So basically you know that to by extending, you should be able to access parent class data into your subclass. For that, simply make the data in your parent class as class level.

class A {
    int var = 10; //class level, but non-static, so to access you need A object

    void method() {
        int var = 20; //this is method local and can not be accessed outside
    }
}

public class B extends A {
    public static void main(String[] args) {
        A aObj = new A();
        System.out.println(aObj.var);
    }
}

Illegal start of expression (when i am delaring variable a as public)

Its illegal. Because access modifiers like public, private etc. are applicable to class-level stuff like the first var or the main method in class B you see.

Said that: You need to immediately go here: https://docs.oracle.com/javase/tutorial/ rather than just trying to run some classes when you lack language basics.

Upvotes: 1

David
David

Reputation: 218867

First, get rid of main() in A. You only want one main() in your application, and it's in B (since the one in A doesn't actually do anything):

public class A {
}

Now, you want A to have a class-level int value:

public class A {
    private int a;
}

And you want it to have a default value of 100, yes? A constructor is a good place to do that:

public class A {
    private int a;

    public A() {
        this.a = 100;
    }
}

Now any time you do this:

A obj = new A();

you will have an object with a value. In order to access that value from outside that object, you need a "getter":

public class A {
    private int a;

    public A() {
        this.a = 100;
    }

    public int get_a() {
        return this.a;
    }
}

Now in B (or anywhere, really), you can create an instance of A and access that value by using the "getter":

A obj=new A();
System.out.println("Variable of class A is :"+ obj.get_a());

Semantically, don't think of it as "accessing a variable from another class". Instead, think of what your objects are and what they represent. If it were a physical, real-world object which internally contained some kind of value.

When you create an instance of that object, the instance would internally have that value somewhere. From the outside of that object, it doesn't really matter how that value is internally maintained. There just needs to be some kind of interface to see the value. Which is what the "getter" method does.

Upvotes: 3

Murat Karagöz
Murat Karagöz

Reputation: 37594

Why are you using the static main method? Besides that the field a is local and not accessible outside the scope. Use this instead.

public class A
{ 
  public int a;
  public A()  
   {
    a=100;
   }  
}

Upvotes: 4

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You don't have two true object-oriented classes above, but rather little more than two receptacles for static main methods. To combine code from two classes well, you will want to scrap that code and make OOP-compliant classes, complete with instance fields and methods. For more on this, check out the OOP section of the Java tutorials: link to OOP tutorial.

Upvotes: 3

Related Questions