Variables is just like math's let x=0. You just add a data type in front of the variable name.
==============================
String x = "hello";
==============================
When you read this, you say a String type variable x has a value of hello.=============================
class DifferentVar{
String instanceVar = null;
static String staticVar = "Endo";public String getName(){
return instances + staticVar;
}
public String getLocal(){
String localVar = "Bell";
return localVar + staticVar;
}public String getParam(String s){
return s + staticVar;
}
}class TestVariables{
public static void main(String[] args){
DifferentVar b1 = new DifferentVar();
DifferentVar b2 = new DifferentVar();b1.instanceVar = "first";
b2.instanceVar = "second";System.out.println(b1.getName());
System.out.println(b2.getName());b1.staticVar = "gold";
System.out.println(b1.getName());
System.out.println(b2.getName());b2.staticVar = "Cranel";
System.out.println(b1.getName());
System.out.println(b1.getLocal());
System.out.println(b2.getParam("L"));
}
}=======≈=======================
Results:
firstEndo
secondEndo
firstGold
secondGold
firstCranel
BellCranel
LCranelDifferent kinds of variables
========≈=====================
1. Instance variables
- known as non static variables.
- this variable is unique to all objects of the same class.
- example, b1 and b2 are two objects with unique instance variable. The variable named instanceVar in b1 is first while in b2 is second2. Static variables
- known as class variable
- this variable is the same to all objects of that same class
- example, when the object b1 declared to change the static variable named staticVar to Gold, it affected the value of object b2 because b1 and b2 are both objects of the same class.3. Local variables
- It has the same semantics with the properties of the class but the location is different. It is called local because it is a variable declared inside the method's body or inside the braces of the method.
- note that if your local variable has the same name with the class properties, the local variable will be prioritise.
- check localVar in getLocal() method above.4. Parameters
- it is a variable declared inside the parenthesis of the method.
- check the variable declared inside the getParam() method
- sometimes this is called arguments. They call the value given to the parameters as arguments to the method.
YOU ARE READING
Java Programming
Randomhttps://docs.oracle.com/javase/tutorial/ This is the link where I learned my java lessons online. I choose java as first programming language. I am a career shifter. I just want to start something new. I choose to write it here in Wattpad so that I...