Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static final

 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Answer given is line 1,2 and 3 all give compile time error.
I think line 1 coz is ambigious, line 3 coz all var in interface are static and final and hence value cannot be changed. But why line 2 as result will be assigned to new var isnt it???
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to complete an expression you shold have both RightHandSide and LeftHandSide proper expression.
Your expressoin is incomplete. You must have some variable at RHS, then this stmt will become complete and will complile.
CMIW
HTH
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thnx that was silly of me isnt it?
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we all do silly mistakes ..
All The Best ..
 
Terrence White
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does
System.out.println (star) ;//1
give an error?
 
Amir Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
star is not declared as static. static methods can only access static members. since main is static, it cannot access the non-static member variable star.
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amir,
But in JLS it is given that
Every field declaration in the body of an interface is implicitly public, static, and final.
http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html#238680
Please clarify.
Thx
Veena
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you compile this code, you should get the following output:

The compiler explanations are self-explanatory...
- on line 1, there is an ambiguous field access (nothing to do with static) (JLS 8.3.3.3 Example: Multiply Inherited Fields)
- line 2 is not a statement as pointed out by Ravish (JLS 14.8 Expression Statements)
- on line 3, we try to assign a value to a final variable. (JLS 4.5.4 final Variables)
[ July 02, 2002: Message edited by: Valentin Crettaz ]
 
Amir Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Veena, forgive my carelessness. I noticed my terrible mistake, right after I hit the "reply post" button.
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amir,
That is ok.We learn by mistakes.
Veena
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic