Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

calling non-static nested class

Thread view: 
Pradyut - 05 Feb 2007 12:58 GMT
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(8));
      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
dagarwal82@gmail.com - 05 Feb 2007 13:04 GMT
> I have two classes under the package javaapplication9
>
[quoted text clipped - 63 lines]
> Pradyuthttp://pradyut.tkhttp://oop-edge.blogspot.com/http://pradyutb.blogspot.com/http://praddy-photos.blogspot.com/http://oop-edge.s
paces.live.com/http://www.flickr.com/photos/praddyhttp://groups.google.com/group
/oop_programming

> India

try this:-
Main.Gear gear = new Main.Gear();
dagarwal82@gmail.com - 05 Feb 2007 13:22 GMT
Correction is above post.
Use this
Main.Gear t =new Main().new Gear();
Lew - 06 Feb 2007 05:56 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.