Results 1 to 6 of 6
  1. #1
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default dont let me create simple class

    hi everyone , this is the code ( hole class , just for testing) :
    Testit is the name of the class with the main method
    Testit1 is sub-class.

    The problem - in the line "Testit1 st1=new Testit1(66);" there is the following error -
    No enclosing instance of type Testit is accessible. Must qualify the allocation with an
    enclosing instance of type Testit (e.g. x.new A() where x is an instance of Testit).
    How to fix it? I dont quite understand even what the problem is

    Java Code:
    public class Testit {
    
    	public class Testit1
    	{
    		public int a1;
    		public boolean b1;
    		public Testit1(int a)
    		{
    			this.a1=a;
    			b1=false;
    		}	
    	};
    		public static void main(String[] args) {
    		
    		int b=1;
    		Testit1 st1=new Testit1(66);
    		if (st1==null)
    			System.out.println("sorry, Testit1 is null");
    		else
    			System.out.println("Testit1 exist and the int is "+st1.a1);
    
    	}
    		
    
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    First of all, Testit1 is not a subclass, it's an inner class. What did you start out to do, make a subclass or make an inner class?

    db

  3. #3
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    Testit1 is not a subclass, it's an inner class.
    You right - my mistake

    Any way, as you can see - it is not a real program. just for me to understand the how classes and arguments and references flow in methods.

    so why
    Testit1 st1=new Testit1(66);
    does not complie?

  4. #4
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    by the way - Testit1 in my code is inner class, makes sense.

    so what is sub-class?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    A subclass is one that extends a class.
    Java Code:
    class SuperClass {}
    
    class SubClass extends SuperClass {}
    Here's a good learning resource:
    Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)

    db

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    And, if you're prepared to devote time to understanding inner classes and enclosing instances:
    Classes

    Considering the present stage of your learning Java (as I perceive it) you might want to just read through that page and come back to it later. You could bookmark this:
    The Java Language Specification, Third Edition - TOC

    db

Similar Threads

  1. Hello! and I need help. I dont know were to start
    By Fall0ut in forum New To Java
    Replies: 10
    Last Post: 05-19-2010, 06:26 PM
  2. 8 questions I dont understand while studying for SCJP
    By shankhas in forum Java Certification
    Replies: 5
    Last Post: 05-19-2010, 07:53 AM
  3. How Can I Create a Simple Grid with a GridLayout?
    By geeeeky.girl in forum AWT / Swing
    Replies: 7
    Last Post: 01-07-2009, 03:31 AM
  4. How to create main class link to another two class?
    By pearllymary78 in forum New To Java
    Replies: 6
    Last Post: 07-16-2008, 11:02 PM
  5. How to create a simple deadlock
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:36 PM

Posting Permissions

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