GerardMT
GerardMT

Reputation: 35

Is it possible to access a variable from another class?

Is it possible to access variable abc directly from a subclass?

I know its possible by changing abc to Static, but I don't want to do this.

main:

public class main {

    public subclass subclass1 = new subclass();
    public boolean abc = false;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        main menu1 = new main();
    }

    public main(){
        while(true){
            if(abc = true){
                System.out.println("true");
            }
        }
    }

}

subclass:

public class subclass {

    public subclass(){
        .abc = true; //possible to access abc of main?
    }

}

Thanks.

Upvotes: 0

Views: 2358

Answers (7)

rgettman
rgettman

Reputation: 178303

Your subclass class isn't subclassing main, so it can't directly access abc. It's confusing to call it subclass, because it subclasses only Object (implicitly).

It needs to have a reference to an instance of the main class, then it can access abc through that instance. That will work because abc is public.

UPDATE

Example:

public class Main
{
    public subclass subclass1;
    public boolean abc = false;

    public static void main(String[] args)
    {
        Main menu1 = new Main();
        menu1.subclass1 = new Subclass(menu1);
        System.out.println(menu1.abc);
    }
}

public class Subclass
{
    private Main myMain;
    public Subclass(Main main)
    {
        myMain = main;
        myMain.abc = true;
    }
}

Upvotes: 2

Jojo John
Jojo John

Reputation: 400

You can access abc in sub class if it extends class main. Please find below a sample

public class Test {
Boolean abc = false;
Test()
{
    if(abc)
    {
        System.out.println("Test():True");
    }
    else
    {
        System.out.println("Test():False");
    }
}
void method()
{
    if(abc)
    {
        System.out.println("Method():True");
    }
    else
    {
        System.out.println("Method():False");
    }
}

public static void main(String[] args) 
{
    Test1 child= new Test1();
    child.method();//Parent method (abc change will reflect)
    Test parent = new Test();//Directly calling parent constructor so abc is false

}

}

child class

public class Test1 extends Test 
{
    Test1()
    {
        this.abc=true;
    }

}

ouput will be

Test():False
Method():True
Test():False

Upvotes: 0

arynaq
arynaq

Reputation: 6870

There are a multitude of things wrong with your code.

  1. Subclass is not a subclass of anything, in order for it to be a subclass it must extend another class by use of the keyword extends, Bus extends Vehicle (by default all classes in java only extend Object).
  2. You have declared abc as public, this means it is accessible to everyone who has an instance of the class main by use of the dot operator on the instance. You can achieve this by creating an instance of main in your subclass

    main m = new main();
    public subclass() {
    
       m.abc = true;
    }
    
  3. You will also have to remove public subclass subclass1 = new subclass(); from main. The way you have made these classes subclass needs main needs subclass needs main needs subclass....circular references.

  4. You will never be able to access an instance of the class because of the while(true) inside the constructor of main. This will run forever and never allow the constructor to finish. You will have to remove the while(true) statement, you can check whether abc has indeed been changed by doing the following

    main m = new main();
    public subclass() {
       System.out.println("Value of abc? "+m.abc);
       m.abc = true;
       System.out.println("Value of abc? "+m.abc);
    }
    

Upvotes: 1

Alowaniak
Alowaniak

Reputation: 600

If you don't make abc static it will only exist in an instance (or object) of "main". So to access it you will need to have a reference to the object. So one thing you could do is ask for Main in SubClass's constructor (you should follow the java conventions) like:

public class SubClass {
    private final Main main;
    public SubClass(Main main) {
        this.main = main;
        main.abc = true;
    }
}

public class Main {
    public SubClass subClass1 = new SubClass(this);
}

or if SubClass is really only for Main's use you could make it an inner class.

public class Main {
    public class SubClass {
        public SubClass() {
            //You can access Main's variables here and in case of ambiguity by doing
            Main.this.abc = true;
        }
    }
}

Alternatively you can create a Main in SubClass.

public class SubClass {
    public SubClass() {
        Main main = new Main();
        main.abc = true;
    }
}

(SubClass naming is a bit weird here and I think you might want to learn a bit more about objects/instances, or OOP in general.)

Upvotes: 0

Anuj Aneja
Anuj Aneja

Reputation: 1344

You can use simple inheritance if both classes are related. Otherwise,

  m.abc= true

Would be a good option.

Upvotes: 0

BlackHatSamurai
BlackHatSamurai

Reputation: 23503

If you are going to access abc, then you would have to have an instance of your main class:

Main m = new Main();
m.abc = "something";

Upvotes: 0

techmonster
techmonster

Reputation: 29

This is Simple inheritance and because abc access modifier is public, you should be able to use in child class without any issue.

Upvotes: 0

Related Questions