File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static - final - obj ref

 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 657
Clojure Spring VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Periakaruppan Thiagarajan
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 657
Clojure Spring VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Output will be false.
Correct. The references point to two different objects. Was that your question?
 
Anupam Bhatt
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic