Win a copy of Spring Boot in Practice 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

Inner Class question Bill Brodgen

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the correct way to instantialze a reference to a NestedClass object.
class BaseClass {
public class NestedClass {
// method and variables for Nested Class
}
}
a) NestedClass myNC = new BaseClass().new NestedClass();
b) BaseClass.NestedClass myNC = new BaseClass().NestedClass();
One more thing can any clarify
when it is mentioned nested class does it many static inner class
and when inner class does it mean non-static inner class.
Kindly explain.
Mahesh
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can answer to the second part -
A nonstatic nested class is called Inner Class and a static nested class is called as a nested class only.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
The second answer is the way for instantiating nestedclass(inner class)
i.e., BaseClase.NestedClass o=new BaseClass().new NestedClass()
There is one more way and that is
BaseClass o=new BaseClass();
NestedClass i=new NestedClass();
i.method()
This is method() may be a method of NestedClass.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to point out that you can't rely on the distinction between the terms "nested class" and "inner class" being used consistently in the Java literature. Frequently I see "Inner class" used for all four types of nested class:
static member class (sometimes called nested top-level class)
member class
local class
anonymous local class
Bill
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic