Question:

In Java, is updating instance variable on a class method possible?

by  |  earlier

0 LIKES UnLike

for example...

public class MyClass{

public int x = 10;

public void function (){

int x = 11;

// how do I store a new value to the original x?

}

}

 Tags:

   Report

2 ANSWERS


  1. u can do this:

    public class MyClass{

    public int x = 10;         //this value of x will be used in this  

    //class except if u give it another value in a block

    public void function (){

    x = 11;           //local variable.can be used in this block only

    }

    }


  2. The answer to your question is no, it is not possible. Your example is unrelated to your question. Your example has an instance method dealing with a local variable that superficially resembles the name of an instance variable. The two x variables are unrelated, and neither can influence the other. For your //: Use this.x = 11;

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.