Reference variables are used to refer to an object. They are declared with a specific type that cannot be changed.
Types of reference variables
- Static Variable
- Instance Variable
- Method Parameter
- Local Variable
Static Variable Example
class test {
//below variable is static variable means it is class level variable
static int i;
public static void main(String[] args) {
// As i is an static variable it can be accessed directly without using any object
System.out.println("Value before calling method1: " + i);
test t1 = new test();
t1.method1();
System.out.println("Value after calling method1: " + i);
t1.method2();
System.out.println("Value after calling method2: " + i);
}
void method1() {
i++;
}
void method2() {
i++;
}
}
Instance / Local / Method Parameter Example
class MPE {
// Instance Variable
int i;
public static void main(String[] args) {
/*
* Here i is an Instance variable.
*/
test t1 = new test();
System.out.println("Value before calling method1: " + t1.i);
}
/*
* Here j is a method parameter. And k is a local variable.
*
* Note**: Local variables life is only till the end of method
*/
void method1(int j) {
int k;
i = j;
/*
* Local Variable(k)'s life ends once execution for this method
* completes. As k is local is variable it needs to be initialized
* before we can use it. But as it is not getting used here, it can stay here without initializing
*/
}
}
Is the final keyword a constant?
It is just keyword can not be marked as constant.
i didn’t got correct information about reference variable
Hi Sirisha,
Let me know what kind of information you think is missing here. Please let me know so that correct the post accordingly.
Can’t I use a reference variable inside the main method?
for eg:
public class Test
{
public static void main(String args[])
{
Test t; //reference variable
System.out.println(t);
}
}
what is the problem with this example?
No. Main is the static method. And static methods cannot use an instance variable. Static methods are not bound to any instance. While instance variables are specific to instances.
Please reply as soon as possible
How to create an object without reference variable? Give 1 example
I didnt get your question. But i believe below article might be helpful to you.
http://94.143.139.145/core-java-tutorial/different-ways-to-create-an-object-in-java/
sir what type of java knowledge is important for android development
Hi Pawan,
Core Java is sufficient to start learning Android. There are some parts of Core Java which can be excluded.
Regards
what
As you said, local variables must be initialized. But you did not initialize the local variable “k”. Am I right?
Statement is correct in sense you can not use any local variable unless and until you initialize it. But you can have instance variable without initializing it if you are not using it. This is the case in this example. k is local variable without initializing.
Is the value of reference variable sets to null??
*static
Hi Navya,
Static variables will have the keyword status meaning it belongs to the class,it is a common variable shared by all instance of the class.so if you like access such a variable you can use any of the object of the same class or the class name itself.
Instance variable alike are instance specific. So every instance of the class has a unique instance variable.they must be accessed by the same instance only
what is the difference between static variable and instance variable
Hi NAVYA,
The Static variable is nothing but preceded by static keyword and its also called class variable whenever .class file is loaded autocratically JVM will allocate the memory for all static member ,and its perform in method area ,
—> you can access this var to four ways :-
Ex:-
class A{
static int number = 10;
psvm(Strin args[]){
A ob = new A();
}
}
1) direct // S.o.p(number);
2) by class name // S.o.p(A.number);
3)by object // S.o.p(new A.number);
4)by object reference // S.o.p(ob.number);
—> but there two ways is not commanded 3>by object and 4>by object reference
ii> Instance variable — the instance variable is nothing but member of class and it define as out side of method ,block and constructor
and memory will allocate for this variable whenever object is created and will destroyed when object is destroy .
it is two ways to access ;-
1)by object // S.o.p(new A.number);
2)by object reference // S.o.p(ob.number);
Static variable belongs to class not object where as instance variable refers to object..
When you call a non-static variable using object…the value of non- static variable remains same until the object is destroyed…
and when next object calls the same instance variable the default value assigned by u is available to the non-static variable..
In static variable with every changed made to the value of static variable the new value is replaced by the old value….
when obj1 calls static variable and make changes to it…this change is stored at the memory location of static variable…
when obj2 calls static variable the new value (made by obj1) is used by obj2..
STATIC variable is only allocated memory once since it belongs to class and when class is loaded the memory is allocated to static variable…
NON-STATIC variable is allocated memory every time when new object is created since it belongs to object not class…..
class MPE {
// Instance Variable
int i;
public static void main(String[] args) {
/*
* Here i is an Instance variable.
*/
test t1 = new test();
System.out.println(“Value before calling method1: ” + t1.i);
}
The above program refers a class test which is actually not a class name.
it should be as
class MPE {
// Instance Variable
int i;
public static void main(String[] args) {
/*
* Here i is an Instance variable.
*/
MPE t1 = new MPE();
System.out.println(“Value before calling method1: ” + t1.i);
}
test class is already defined in above example.
sir,
i m a java beginner.I just want ask u that can we change the value of variable which is declared as static and why static keyword is used?
Hi Ashish,
Sorry for late reply. Yes you can change the value of Static variable. Please follow below link to understand
STATIC keyword.
http://94.143.139.145/core-java-tutorial/java-static-keyword/
Regards
Vivekanand Gautam
In the second example, the class name has changed from ‘test’ to ‘MPE’, but on line 9, we see this:
test t1 = new test();
Shouldn’t the creation of the t1 object look like this:
MPE t1 = new MPE();
Can I write like this ?? Is this a valid ??
Class test {
Static int i ;
i=20;
}
NO
why???????
U can write…
because there is two variable with same name in same class so it conflict
is final is a reference variable or not plz suggest
thanks
Final is a keyword work as Non Access Modifier. It is not a variable in itself. But can be applied to Instance Variable.
sir,cud u pls let me knw : how a static variable can be accessed? To be clear,is it accessed via an obj or without any object?
//Static variable can accessed using Class name or using an instance of class object //example :
class Test
{
static int var;
public static void main (String[] args)
{
//————–Example1 using class name—————-:
Test.var = 5;
System.out.println(Test.var); // output =5
//————/Example2: Using Class instance:—————
Test objTest = new Test();
objTest.var =10;
System.out.println(Test.var); // output =10
}
}
A reference is what is used to describe the pointer to the memory location where the Object resides
I want more information about the referance variable. because not got fully. please
example please
Hi Shiva,
Article has been updated with proper example. Please check again.
Thanks
can you make more example and harder one?
We can try