Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

interface

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test6 implements I{
int k = 1;
public static void main(String args[]){
System.out.println(k);
}
}
interface I{
int k = 0;
}
What will be the output?
A1) 0
A2)1
A3)null
A4)Compiler Error
#####################################333
the answer is [4]
How?
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi hemamalini s.,
You cannot access a non-static member in a static context.
If you want to access the variable "k" in the interface I then you would have to use this:
System.out.println(I.k);

You can access "k" of interface I in the above manner as member variables of an interface are implicitly public static final.

Regards,
Saurabh
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My compiler, javac 1.4.2, gives an error when asked to "System.out.print(l.k)".
I can comment out the instance variable, in which case "System.out.print(k)" prints "0", or I can create an instance of the class in "main()", call it "I", and "System.out.print(I.k)" prints "1". So, I would say the correct answer, in 1.4, is "Compiler Error".
 
Saurabh Chaubey
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Michael,
I am also mentioning that answer to question posted would be "Compile time error". But to access the variable k of the interface the following code will work:

public class Test6 implements I{
int k = 1;
public static void main(String args[]){
System.out.println(I.k);
}
}
interface I{
int k = 0;
}

//Output:0
 
Joseph Clark
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right. I mistook the uppercase "I" for a lowercase "l".
"System.out.print(I.k)" prints "0".
 
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sure is a tricky quesion. Thanks for posting.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic