简体   繁体   中英

How can i access one class variable into other

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. My program is very lengthy ,I just want the logic of this.Thanking you in advance.

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 variable a as Static so that i can directly get access but it's not working. and when i am compiling B.java It 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 delaring variable a as public )

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

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;
   }  
}

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM