CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Memeber Class

  1. #1
    Join Date
    Sep 1999
    Posts
    7

    Memeber Class

    I have written following code. JDK is giving some error message. I am unable to correct it.
    Help me.

    Code is

    import java.lang.* ;


    public class Sample2
    {

    public static void main(String[] args)
    {
    f1();
    }
    class a1 { };

    static void f1()
    {
    a1 x1;
    x1 = new a1();
    }

    }

    Compiler Error :
    No enclosing instance of class Sample2 is in scope; an explicit
    one must be provided when creating inner class Sample2. a1, as in "outer. new I nner()" or "outer. super()".
    x1 = new a1();


  2. #2
    Join Date
    Sep 1999
    Location
    Mainz, Germany
    Posts
    20

    Re: Memeber Class

    Hi

    Ähmmmm what do you try with this ?
    If you tell me what you wanna do, than I can help you but that is total false.
    At first:
    Every class is it own file
    So if you wanna make an instance of the class A1 than it must be a own file
    but I don´t understand what to the hell that program should do ?!?


  3. #3
    Join Date
    Sep 1999
    Posts
    7

    Re: Memeber Class

    I have a declare a member class in main class.
    Now i want to create a instance of that member class in the main class , is it possible ,
    on which places i can create the instance of that member class


  4. #4
    Join Date
    Sep 1999
    Location
    Mainz, Germany
    Posts
    20

    Re: Memeber Class

    OK
    If you want to create an instance of a class, than you need at first another class. So if you want to try it, than you can to it like that:
    Make a File named Test1.java
    in that one you just write
    public class Test1
    {
    int Number1;
    int Number2;
    }

    than you create a second file named Test2.java
    there you write:
    public class Test2
    {
    public static void main (String args[])
    {
    Test1 a = new Test1();
    a.Number1 = 5;
    }
    }

    So you have created an instance of the class Test1 named a. Every class must be a own file ! ! !


  5. #5
    Join Date
    Sep 1999
    Location
    Dubai, UAE
    Posts
    38

    Re: Member Class

    Ashwani,

    Leaving what you are trying to do apart, the concept is something like this - when u create a class that is a member of another class - it behaves exactly the same way as a variable does. Meaning, if u have a non-static variable in a class, u cannot reference it from inside a static context. Say, u hv 2 classes Outer and Inner, if class Inner is not static, it cannot be referenced from inside a static context of Outer. ( Check up the implicit - "this" - context topic. For ur code, the correction u've to do is simple -


    import java.lang.* ;

    public class Sample2
    {
    public static void main(String[] args)
    {
    f1();
    }

    class a1 { };

    static void f1()
    {
    // Note : Function f1() is static. There is no "this" applicable here of class Sample2.
    // An Inner class, if not static, needs an enclosing instance of the Outer class - or
    // the function that does the instantiation should not be a static function.
    Sample2 s = new Sample2();
    a1 x1;
    x1 = s.new a1();
    // Now, if, in class a1, if u have referenced some variables belonging to class Sample2,
    // they'll have a "this" context applicable to them ( s ).
    }
    }





    Contact me if u have further clarifications.
    Rgds,

    R.Saravanan


  6. #6
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Memeber Class


    > So if you wanna make an instance of the class A1 than it must be a own file

    Erbeere, Have you heard about inner classes ? She is trying to learn something ..
    Who said that every class has it's own file ? We can put 'n' number of classes inside
    a single java file. BUT YOU CANT PUT MORE THAN ONE "PUBLIC" CLASS INSIDE A JAVA FILE.

    > but that is total false

    That's why she has posted this question. Otherwise she wont...

    I agree with Saravanan.

    One more Solution is , make the al class as a static variable in your sample2 class...


    import java.lang.* ;


    public class Sample2
    {

    public static void main(String[] args)
    {
    f1();
    }
    public static class a1 { };

    static void f1()
    {
    a1 x1;
    x1 = new a1();
    }

    }




    Poochi..


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured