aspose file tools*
The moose likes Beginning Java and the fly likes static - final - obj ref Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static - final - obj ref" Watch "static - final - obj ref" New topic
Author

static - final - obj ref

Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129
Hi,

I know the keyword final is used for:

In Class : It can't extended
In Method : It can't overridden
In Variables : It can't changed (i.e., constant)

I know the static is used for:

In Class : No need to create an object.
In Method: Can be called with help of class name(no need of object)
In variable: Shared by all the objects of the class.

My doubt is what will be the meaning of the below coding:

1. final Obj o1 = new Obj();
2. static Obj o1 = new Obj();
3. static final Obj o1 = new Obj();

I am very confused with static and final while I used with the object reference.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

1. final Obj o1 = new Obj();

The o1 variable cannot be reassigned another reference.

2. static Obj o1 = new Obj();

The o1 variable is shared among all instances of the class in which it is declared.

3. static final Obj o1 = new Obj();

The o1 variable is shared among all instances of the class in which it is declared and cannot be reassigned another reference.
Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129

1. final Obj o1 = new Obj();

The o1 variable cannot be reassigned another reference.


Can you give me an example how the variable can be reassigned another ref without using final.

Ex:

Obj o1 = new Obj();
Obj1 o1 = new Obj1();

Is it possible?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Your last example tried to make two variables with the same name. The bit you're looking for - changing the object that a variable refers to - is so simple you looked right by it:

String s = "hello";
s = "goodbye";

Try this with s as final and see what the compiler says.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Periakaruppan Thiagarajan
Ranch Hand

Joined: Jul 26, 2005
Posts: 65
Originally posted by Niyas Ahmed Sheikh:


Can you give me an example how the variable can be reassigned another ref without using final.

Ex:

Obj o1 = new Obj();
Obj1 o1 = new Obj1();

Is it possible?


Try to execute the following piece of code and see it what happens?
Object obj1 = new Object();
Object obj2 = obj1;
obj1 = new Object();
System.out.println("Is it Equal ? = "+(obj1 == obj2 ));

Output will be false.

[ July 28, 2005: Message edited by: Periakaruppan Thiagarajan ]
[ July 28, 2005: Message edited by: Periakaruppan Thiagarajan ]
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Output will be false.
Correct. The references point to two different objects. Was that your question?
Anupam Bhatt
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
Originally posted by Periakaruppan Thiagarajan:


Try to execute the following piece of code and see it what happens?
Object obj1 = new Object();
Object obj2 = obj1;
obj1 = new Object();
System.out.println("Is it Equal ? = "+(obj1 == obj2 ));

Output will be false.


That true .. and i think if we change the first statement to >>

final Object obj1 = new Object();

then there would be an compile-time error at >>
obj1 = new Object();
Correct me if wrong.

Periakaruppan Thiagarajan
Ranch Hand

Joined: Jul 26, 2005
Posts: 65
Originally posted by Anupam Bhatt:


That true .. and i think if we change the first statement to >>

final Object obj1 = new Object();

then there would be an compile-time error at >>
obj1 = new Object();
Correct me if wrong.



Thats true anupam. It results in compliation failue. Final variables cannot be reintialized once assigned a value.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Niyas Ahmed Sheikh:


I know the static is used for:

In Class : No need to create an object.



You can't use static with class name untill it is not inner class.
Anupam Bhatt
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
Originally posted by rathi ji:



You can't use static with class name untill it is not inner class.


Thats rightly pointed out.....

Meanwhile thanks Periakaruppan... to confirm my inference.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: static - final - obj ref