• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Marcus mock exam 2 (quest. #32)

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm confused about the inner class being declared private. Here's the question:
What will happen when you attempt to compile and run this program:
public class Outer
{
public String name = "Outer";
public static void main(String argv[])
{
Inner i = new Inner();
i.showName();
}//End of main
private class Inner
{
String name =new String("Inner");
void showName()
{
System.out.println(name);
}
}//End of Inner class
}
1) Compile and run with output of "Outer"
2) Compile and run with output of "Inner"
3) Compile time error because Inner is declared as private
4) Compile time error because of the line creating the instance of Inner
The correct answer is option 4. I thought that the program would also generate a compile time error because Inner is declared as private, but it seems to work fine. Why is this the case?
Pls help.
Thanks.
Mansi
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mansi,
main() method is static and can access oONLY static methods and variables directly.
For the given program, you can have to create an instance of the Outer class to access the inner class.
Inner i = new Outer().new Inner();
But, if the Inner class was static, then main() could directly create an instance of the Inner class.
private static class Inner
{
..
}
Hope this helps;-)
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the error message I got when I compile
Outer.java:5: non-static variable this cannot be referenced from a static context
Inner i = new Inner();
^
1 error
in order to make this code work we need to add static in the inner class then only static void main method can access inner class. static only acces static!
here is working code!
public class Outer {
public String name = "Outer";
public static void main(String argv[])
{
Inner i = new Inner();
i.showName();
}//End of main
private static class Inner {
String name =new String("Inner");
void showName()
{
System.out.println(name);
}
}//End of Inner class
}
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !
This is regarding your question on inner classes.
public class Outer
{
public String name = "Outer";
public static void main(String argv[])
{
Inner i = new Inner();
i.showName();
}//End of main
private class Inner
{
String name =new String("Inner");
void showName()
{
System.out.println(name);
}
}//End of Inner class
}
The program would give a compiler error on the line Inner i= new Inner() because this is possible only if the instantiation is done from inside the outer class instance code.
But here the instantiation is done within the main method which is static and so the correct way of instantiating it would be
Outer.Inner i=new Outer().new Inner();
So the rule is
From outside the outer class instance code(including static method code within the outer class), the inner class name must include the outer class name (Outer.Inner) and to instantiate ,you must use a reference to the outer class(i.e new Outer().new Inner() )
Inner classes can be marked private and it becomes a private member of the outer class.The code would work fine if the inner class was made static b'cos main can access static members directly.
This is the explanation according to the K&B book.Hope I made myself clear and if it's wrong please correct me.
Good luck.
[ November 18, 2003: Message edited by: Sagarika nair ]
 
No. No. No. No. Changed my mind. Wanna come down. To see this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic