• Post Reply Bookmark Topic Watch Topic
  • New Topic

Very basic questions about variables and final keyword in java  RSS feed

 
Vinod Vinu
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i have two basic questions.

Ques 1. Suppose there is a seperate independent class called Person and if i write like this :-



then what does 'p' stands here ? Is 'p' a variable of class 'Person' or is this the object like when we
write


Ques 2. This is related to 'final' keyword in java.
I know that when a final is used with a class then it cannot be further inherited, when it is used in varible then that
becomes constant and when used with a method then that method cannot be overloaded. What about when it is used with
a object ? like



This statement is correct and does not give any compilation error but what significance it has ?

Can we write something like this :-




Please help me to clear my basics doubts.
Thanks in advance for your valueable time.
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Your first Question
p is a refrence variable of type person .
And when we write new it allocates memory for p .
Until that no object is created .
Question 2 :
When we use final with person , it states that refrence variable person cannot be deferenced or referenced to some other object .

Also when we use final with a method it cannot be overridden.
It can definately be overloaded.
 
Vinod Vinu
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also when we use final with a method it cannot be overridden.
It can definately be overloaded.


Thank you so much Pratik. Till now i was wrong about final keyword with a method.
I will remember in future that a final method can be overloaded but it cannot be over-ridden.

Thanks for clearing all my doubts about the reference variable also.
 
swaraj gupta
Ranch Hand
Posts: 186
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1> p is reference variable of type Person which is not referring to any object of class Person on the heap.( as objects reside on heap)
2> Once you declare a reference variable as final, then you can not make that reference refer to a new object on the heap though you can change the object referred to.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really mean "dereferenced", Pratik D mehta. That usually means getting the contents out of a pointer, and is not a word usually used in Java. Apart from that, you are both correct. Remember that if Person is a mutable class, you can alter the state of a reference type pointed to by a final reference.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use final with a method parameter, or with a local variable. Or with a field which is a primitive, in which case it becomes a constant.
You can use final with a class, in which case you must not extend that class.
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok , yes Campbell that would be in C i think.
The word here would be assigned , that is the reference variable cannot be assigned to another object .
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree; "reassign" is a better term, and dereference is in common use in C. You occasionally see it in compiler errors when you try i.foo() where i is a primitive.
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes , Thankyou
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!