• 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

about inner class

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a few doubts about inner class. I would be happy if anyone would clear my doubts.
1. Does the inner class accessible from other class ?
2. Can an inner class be instantiated in other class ?
Thanks in advance
usman
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Does the inner class accessible from other class ?
Instances of the inner class can be accesible depending on the modifier you apply to that instances.
2. Can an inner class be instantiated in other class ?
Yes, if you have instantiate the Outer class. There is only one case when you can intantiate an Inner class without the instatiation of the Outer, that case is when the Inner class is declared static.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hope the following example Test.java clears your doubt.
- Srini
public class Test {
public static void main(String args[]) {
Outer out=new Outer();
Outer.Inner in=out.new Inner();
System.out.println(out.outerVariable);
System.out.println(in.innerVariable);
}
}
class Outer {
public int outerVariable=10;
public class Inner {
public int innerVariable=20;
}
}
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marcela Blei:
There is only one case when you can intantiate an Inner class without the instatiation of the Outer, that case is when the Inner class is declared static.


Inner classes are NEVER static - they are called Nested classes

 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak M:
Inner classes are NEVER static - they are called Nested classes


What do you mean? I don�t understand, the following code compiles fine:
public class Outer {
static class Inner {
Inner() {
System.out.println("Inner como member de la clase");
}
}
}
Please explain the difference between Nested and Inner if the point is there!
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the types of inner classes are:
1. static top level inner class (also may be called as top level nested class
2. non-static inner class (i think this is also called as simply nested class in a lot of text)
3. local inner class (these may be implicitly static depending upon the declaration context(like static method) they may not be declared static, however))
4. anonymous class (same condition as pt. 4)
static inner classes definitely exist. in fact, you may get a question on the test asking your opinion if a class may be declared static. the answer is yes, a class may be declared static, but only as a static inner class
regards...
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marcela,
The code u have given is actually defining a static nested class named inner and not an "inner" class (note: inner is not a keyword in java) which is a non static nested class.
The key difference bet static nested class and an inner class is that -
The static nested class is tied only to the outer class, not an instance of the outer class.
Eg : static variables exist for the class and not for every object of the class.
But member (i.e. non static class level) variables declared for the class exist for every object- i.e. each object has its own copy of member variables.
That's why you can make an instance of the static nested class without having an instance of the outer class, just the way you can call static methods of a class without having any instances of that class.
eskay
 
mohammed usman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for one and all who have cleared my doubt.
regards
usman
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic