• 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

question

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interface inter
{
String str="vow";
}
class gen
{
String str="super";
}
public class test extends gen implements inter
{
public static void main(String args[])
{
System.out.println(str);
}
}

I saw this question in some mock before two days.I forgot its name.Sorry.

What will be he output.
The answer given was "jvm will be confused"
It can only access inter's str right because that alone is implicitly static while str of gen needs an instance to invoke.

Please clarify

Thanks
Praveen SP
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi praveen

When you compile this code , you got the exception

reference to str is ambiguous, both variable str in testproject.gen1 and variable str in testproject.inter match

System.out.println(str);

1 error

Because str have both class and interface.

You should mentioned, whether str should be the interface or class.
 
Praveen Seluka
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I did not understand the explanation given.
System.out.pritnln(str);

"this str refers to str of inter since its called from the static context,and if we need to access gen's str we need an instance"

Please clarify
Thanks
Praveen SP
 
krishnamoorthy kitcha
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi praveen

If you want access the string in the interface

Just give the System.out.println(inter.str)

or else
You want access the string in the class

Just create the class instance , then access the string.
Class gen a = new gen();

a.str;

System.out.println(a.str)


Do you want understand ? Why we are creating the instance in the class but not in the interface.

Because interface motheods and members are always public and static.

But in the class you are defined defeault.
 
Praveen Seluka
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi jesper young

I did not post it twice.Dont know how it got posted twice.

And I shall give a valid subject hereafter

Thanks
Praveen SP
 
Did you miss me? Did you miss this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic