• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

interface constants over ride

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


As s1 in I is implicity public static final (as it is a constant), how come
A modifies it ? javascript: x()
banghead
 
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pillai ,
As we know that final fields were not intended to change .
in case of your snippet s1 declared in interface is different from the implemented class 's s1 .


I had included the Java Language Specfication for reference ,Hope this will help you .
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

Gowtham wrote:

System.out.println(finalStr);
//above line will results compile error due to ambiguous


There's no ambiguity in here. The line will not compile because you are trying to access a non-static variable from static context.


Also in Sweta's example there is no ambiguity.
Also �8.3.3.3 has nothing to do with it.

Sweta asked:

As s1 in I is implicity public static final (as it is a constant), how come A modifies it ?



I guess, Sweta is mixing up the two different finals:

A final method cannot be overridden.
A final variable cannot be reassigned. But in a class that inherites the variable, you can define a new variable with the same name.

In Sweta's example the situation is even more complicated:
Variable s1 in interface I is implicitely static. So even though you don't read the "static" keyword in the code, it is there. Along with final.

In the implementing class, you have a non-static variable with the same name. Therefore, the static variable cannot be assigned as A.s1. Would be referencing non-static from static ... the classic fault.
But it can still be refernced through the interface, thus


prints
I
A



Yours,
Bu.
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

apologies from my side ,as i didnt notice , i am refering the non static var in static method .

i do agree with Burkhard
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Gowtham,

no needs for apologies here. The code from Sweta is in the category "looks easy, is tricky".
I really want to know, how many cowboys make mistakes in trying to access a non-static variable from a static context in the real exam.
Say, after properly answering questions for over an hour...

Yours,
Bu.
 
Can you smell this for me? I think this tiny ad smells like blueberry pie!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic