Search from various English teachers...
Myanmar (Burma) flaglouie
What is an instance variable in Java? /* As a programer for 20 years, I think the most important thing in programming * is to manipulate the variables in a program. * * I am learning how to develop a Java program, so I want to know what's the * coding rules of variable in Java programming language. * * In other programming languages like coble or RPG, we declare a variable * once in a program then we can use the variable in anywhere of that program, * but in java, it's totally diffirent due to it's a object oriented programming * language, there are 3 kinds of variable in Java: * * 1. local variable. * 2. instance variable * 3. static/class variable. * * And now I just know they are located in diffirence places in the program code * and I want to know the detail, so the following is the code that can help me * to know what the instance variable is. * * In this class, I want to manipulate the instance variable and * to see what the instance variable is. * */ /** * * @author Louie Chen 2012/11/13 13:16 */ public class InstanceVariable { /** * The variables that I place in this position are called instance variables. */ int x = 0; /** * Adding 2 methods that use the instance variable x, by looking * the variable-x's value to understand what is a instance variable, * and how that can be used in a program. */ /** * The fist method is named methodA */ void methodA(){ x = x + 1 ; System.out.print(x); } /** * The second method is named methodB */ void methodB(){ x = x + 1 ; System.out.print(x); } public static void main(String argu[]){ InstanceVariable test = new InstanceVariable() ; test.methodA(); test.methodB(); } /** Do you really want to guess what's the result after I run the program? * Actually I don't really know what's the result brfore I run, but I guess * the result will be 12, and you know what? I am right, the result is 12. * that can explain why it called a instance variable. */ }

Corrections · 3

0

What is an instance variable in Java?

 

/* As a programer for 20 years, I think the most important thing in programming
* is to manipulate the variables in a program.
*
* I am learning how to develop write a Java program Java and so I want to know what's are the coding rules of in using variables in Java programming in this language.
*
* In other programming languages like coble COBOL or RPG, we declare a variable
* once in a program then we can use the variable in anywhere of that program. These * languages has only global variables.  However, but in Java, it's totally diffirent due to * it's a object oriented programming and other modern programming languages we

* have scoping rules to limit the life time of variables as use global variables can be extremely dangerous.

* language, There are 3 three kinds of variables in Java:
*
* 1. local variable.
* 2. instance variable
* 3. static/class variable.
*
* And now I just know they are located in diffirence places in the program code
* and I want to know the detail, so the following is the code that can help me
* to know what the instance variable is.
*
* In this class, I want to manipulate the instance variable and
* to see what the value of the instance variable is.
*
*/

/**
*
* @author Louie Chen 2012/11/13 13:16
*/
public class InstanceVariable {

/**
* The variables that I place in this position are called instance variables.
*/
int x = 0;

/**
* Adding 2 two methods that use the instance variable x, by looking
* the variable-x's value to understand what is a instance variable,
* and how that can be used in a program.
*/

/**
* The fist method is named methodA
*/
void methodA(){
x = x + 1 ;
System.out.print(x);
}

/**
* The second method is named methodB
*/
void methodB(){
x = x + 1 ;
System.out.print(x);
}

public static void main(String argu[]){

InstanceVariable test = new InstanceVariable() ;
test.methodA();
test.methodB();
}

/** Do you really want to guess what's the result after I run the program?
* Actually I don't really know what's the result brfore I run, but I guess
* the result will be 12, and you know what? I am right, the result is 12.
* that can explain why it called a instance variable.
*/

Why 12?  x was given an initial value of 0.  You only called the methods twice.  Even though I don't know Java, logic tells me that 12 is a wrong answer.  Nowhere do I see a value of 10.

 

From what you wrote, an instance variable is nothing more than a local variable common to the instance of an object.  This can be also very dangerous if care is not exercised.  My programming philosophy is to use local variables only unless the application truly needs it.  For example, the seed in a random number generator package (In Ada's terminology).

}

Want to progress faster?

Join this learning community and try out free exercises!

Don’t miss out on the opportunity to learn a language from the comfort of your own home. Browse our selection of experienced language tutors and enroll in your first lesson now!

Start Learning Today