• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static final

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnx that was silly of me isnt it?
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we all do silly mistakes ..
All The Best ..
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does
System.out.println (star) ;//1
give an error?
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amir,
That is ok.We learn by mistakes.
Veena
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic