2

How to get a value which declared and initialized in another method without using parameter in java?

public class Value {
    void method1()
    {
        int demo=10;
        System.out.println("methd 1"+demo);
    }
    void method2()
    {
        System.out.println("method 2"+demo);
    }

    public static void main(String[] args) {
        Value obj = new Value ();
        obj.method1();
        obj,method2();
    }
}

Here the variable demo is declared in method1 and assigned a value now I need to get the value of demo in method 2 is this possible without any parameter, global declaration, and no getter setter method?

4
  • 1
    method2() has no value for demo, as it isn't defined. In main(), you have obj,method2(), which should be obj.method2() Commented Jul 11, 2015 at 10:09
  • you should learn variable scope this is very basic. Commented Jul 11, 2015 at 10:21
  • @Rustam you shouldnt criticize people for trying to learn. Commented Jul 11, 2015 at 10:22
  • Why do you want to do it without the things you listed? What's your actual end goal here? Commented Jul 11, 2015 at 10:35

5 Answers 5

6

No, it's not possible, because demo doesn't exist once method1 has returned. It's a local variable within method1.

...without any parameter,global declaration, and no getter setter method?

That pretty much rules everything out, if by "global declaration" you mean making demo an instance field (which isn't a global, but I think that's what you meant).

But just for completeness, here's demo as an instance field:

public class Value {
    private int demo;

    void method1()
    {
        this.demo = 10;
        System.out.println("method 1" + this.demo);
    }
    void method2()
    {
        System.out.println("method 2" + this.demo);
    }

    public static void main(String[] args) {
        Value obj = new Value ();
        obj.method1();
        obj.method2();
    }
}

You're not required to use this. when accessing it, but doing so makes it clear it's an instance field, not a local variable.

Sign up to request clarification or add additional context in comments.

Comments

3
public class Value {
    void method1()
    {
        int demo=10;
        System.out.println("methd 1"+demo);
    }
    void method2()
    {
        System.out.println("method 2");
        this.method1();
    }

    public static void main(String[] args) {
        Value obj = new Value ();
        obj.method1();
        obj.method2();
    }
}

or

public class Value {
int method1()
{
    int demo=10;
    System.out.println("methd 1"+demo);
    return demo;
}
void method2()
{        
    int demos = this.method1();
    System.out.println("method 2 "+demos);
}

public static void main(String[] args) {
    Value obj = new Value ();
  //  obj.method1();
    obj.method2();
}

}

Comments

0

Actually

int demo

in method1 is just local to the function method1. I don't know what java compiles from such code, but you can be sure, that such a variable will simply go into a register when you use a compiler that creates bytecode for your processor. You just use this as a convenient name for a variable.

If you want to use the variable in other methods, you need to share this value as data member in class Value.

Comments

0

Although local variables of methods are being discarded at compile time, you can get them if you compile with debug information javac -g. You can take advantage of the bytecode libraries ASM or BCEL. If you suspend your JVM by the debug agent you can also exploit a remote debugger API.

Comments

0
public class Value {
    int demo;
public int getDemo(){return demo;}
public void setDemo(int demo){this.demo=demo;}

    void method1() {
       // demo=10;
        System.out.println("methd 1"+demo);
    }

    void method2() {

System.out.println("method 2"+demo);
    }

    public static void main(String[] args) {
        Value obj = new Value ();
//set the value of demo using setter method        

obj.method1();
        obj.method2();
    }
}

1 Comment

True, but he didn't want getters & setters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.