• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instantiating a class is not working  RSS feed

 
narendra nath
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote all code in one file. I have attached the file also.

 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what goes wrong? I can see nothing wrong with the code, but:-
  • 1: At this stage, miss out the package name.
  • 2: It is never necessary to import java.lang.Anything;
  • You will have problems if you use a package name, because you have to put the .class files in a particular folder. Miss out the package name and use the unnamed package for the first few weeks.
    I tried your code. You are short of a } before the beginning of the Testing class.

    Also use code tags: Liutauras has added them for you and doesn't it look better
     
    Liutauras Vilda
    Sheriff
    Posts: 4930
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi basam nath,

    When you post code on JavaRanch, please use code tags, it helps to make your code more readable.
    I have added them for you this time. Please check how to UseCodeTags (<- link) for the next time.

    Also, it is essential to go through TellTheDetails (<- link) tutorial.
    Remember, you have to be specific, otherwise there is no way to help you, unless you provide clear explanation, what kind of issues you're facing.
     
    narendra nath
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    can you folks kindly point me to any example or video that shows how to create a new class file, and instantiate an object of that class in static Main function.

    Your advise and suggestion is always greatly appreciated

    thaanks
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16060
    88
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It doesn't work because your class Testing is a non-static inner class. You can only create instances of a non-static inner class in the context of an instance of the enclosing class. The main method is static, so it doesn't have a current instance of the enclosing class.

    You can either make the inner class Testing static, or create an instance of it in the context of an instance of the enclosing class, which means you create the instance inside a non-static method, or on an instance of the enclosing class.

    or like this:

    or:
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think it is simpler to put a } before the Testing class and convert it to a top‑level class.
     
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    First op should clear that whether he is using Testing as Inner class or not
     
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Look if you are using inner class
    Then try not instantiating it inside any static method (not even main) then you'll have to worry about the reference of outer class (atleast that what helped me)
    Its better try making "main" light enough that maximum it contains a line or 2
    If you had came from c background then it is obvious you'll be having problem of putting every thing to main
    But don't do it
     
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have made certain rules for myself while using inner class .Hope it will somehow help you
  • If you are having an inner class(non static)
  • you want to use it inside the outer class

  • Then make object of inner class in data field of outer class ,if you want to use any method of inner class in outer class then create non static method of outer class in it use reference id of inner class to call its method, and in main as i promised you that I will make it light,so in it I'll create object of outer class and call its non static method
  • if you want to use inner class in any other class except outer(ya!! I am not using nested class for what purpose they are meant for.I haven't made it private)

  • Create an interface with all abstract method whose implementation is in your inner class
    Implement interface by your class
    And make a non static method in outer class with return type of interface and return the reference id of inner class in it
    Now in that other class make an object of outerclass and a non static method in which call the method returning reference I'd
    And use that reference id to call its method in main as i promised you that I will make it light,so in it I'll create object of other class and call its non static method
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Forget about inner classes. It is a typing error. Too few }s before the TEsting class and too many }s after it.
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!