Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > calling non-static nested class

Reply
Thread Tools

calling non-static nested class

 
 
Pradyut
Guest
Posts: n/a
 
      02-05-2007
I have two classes under the package javaapplication9

the first class: -
package javaapplication9;

/**
*
* @author Administrator
*/
public class NewClass {

/** Creates a new instance of NewClass */
public NewClass() {
}
public int addn(int i)
{
return i + i;
}
}
-----------------------------------------------------------------

The second class: -
package javaapplication9;

/**
*
* @author Administrator
*/
public class Main {

/** Creates a new instance of Main */
/*static Gear obj = new Gear();*/
public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("test");
NewClass obj = new NewClass();
System.out.println(obj.addn();
Gear t =new Gear();
t.Jam();
}

public /*static*/ class Gear
{
public void Jam()
{
System.out.println("Testing");
}
}
}
-----------------------------------------------------------------

The problem:-
I can call non-static class NewClass from main and not the non-static
class Gear
Why??
Any other solution than declaring Gear static

Thanks

Pradyut
http://pradyut.tk
http://oop-edge.blogspot.com/
http://pradyutb.blogspot.com/
http://praddy-photos.blogspot.com/
http://oop-edge.spaces.live.com/
http://www.flickr.com/photos/praddy
http://groups.google.com/group/oop_programming
India

 
Reply With Quote
 
 
 
 
dagarwal82@gmail.com
Guest
Posts: n/a
 
      02-05-2007
On Feb 5, 5:58 pm, "Pradyut" <(E-Mail Removed)> wrote:
> I have two classes under the package javaapplication9
>
> the first class: -
> package javaapplication9;
>
> /**
> *
> * @author Administrator
> */
> public class NewClass {
>
> /** Creates a new instance of NewClass */
> public NewClass() {
> }
> public int addn(int i)
> {
> return i + i;
> }}
>
> -----------------------------------------------------------------
>
> The second class: -
> package javaapplication9;
>
> /**
> *
> * @author Administrator
> */
> public class Main {
>
> /** Creates a new instance of Main */
> /*static Gear obj = new Gear();*/
> public Main() {
> }
>
> /**
> * @param args the command line arguments
> */
> public static void main(String[] args) {
> // TODO code application logic here
> System.out.println("test");
> NewClass obj = new NewClass();
> System.out.println(obj.addn();
> Gear t =new Gear();
> t.Jam();
> }
>
> public /*static*/ class Gear
> {
> public void Jam()
> {
> System.out.println("Testing");
> }
> }}
>
> -----------------------------------------------------------------
>
> The problem:-
> I can call non-static class NewClass from main and not the non-static
> class Gear
> Why??
> Any other solution than declaring Gear static
>
> Thanks
>
> Pradyuthttp://pradyut.tkhttp://oop-edge.blogspot.com/http://pradyutb.blogspot.com/http://praddy-photos.blogspot.com/http://oop-edge.spaces.live.com/http://www.flickr.com/photos/praddyhttp://groups.google.com/group/oop_programming
> India


try this:-
Main.Gear gear = new Main.Gear();

 
Reply With Quote
 
 
 
 
dagarwal82@gmail.com
Guest
Posts: n/a
 
      02-05-2007
Correction is above post.
Use this
Main.Gear t =new Main().new Gear();

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-06-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Correction is above post.
> Use this
> Main.Gear t =new Main().new Gear();


Explanation: Gear is an inner class, and needs an object of its outer class to
give it nutrients. "new Main()" makes an object of the enclosing class, which
object owns the non-static inner class, so from the object you call "new
Gear()". Your original code did not have an enclosing object around the "new
Gear()".

Incidentally, the virtually universal and Sun-endorsed convention for Java
nomenclature is to name classes with an initial upper-case letter, methods and
non-static-final variables with an initial lower-case letter. The method
"Jam()" would conventionally be named "jam()".

- Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling Ruby module methods from a nested class within mgerstenblatt@gmail.com Ruby 1 03-16-2011 02:36 PM
Nested friend class in nested template problem tonvandenheuvel@gmail.com C++ 3 12-07-2007 03:02 PM
calling 'GetLocalResourceObject' from a nested class ANeelima ASP .Net 1 12-04-2007 01:01 AM
Is nested class automatically friend of class that it is nested in? request@no_spam.com C++ 5 09-25-2006 08:31 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM



Advertisments