• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why cant i instantiate this class?

 
jon ninpoja
Ranch Hand
Posts: 184
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
was just experimenting...well trying to...with objects and classes

why cant i instantiate this class?
sure i will kick myself when you tell me!



on this line:
MyObjects objectOne = new MyObjects();
i get intelisense...
cannot be referenced from a static context

i mostly have my classes in separate files...but for small examples and practice i dont find this practical

thanks
 
Henry Wong
author
Marshal
Pie
Posts: 22092
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your MyObjects class is an inner class of your ObjectReferenceVariables class. So, in order to instantiate a MyObjects instance, you need an instance of a ObjectReferenceVariables class type.

Of course, if you haven't learned inner classes yet, and is only defining the class within a class, because it is more practical; well, as you just noticed, it is not as simple as you think it is...

Henry
 
jon ninpoja
Ranch Hand
Posts: 184
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi henry,

why doesnt this work?
ObjectReferenceVariables outerClass = new ObjectReferenceVariables();
        outerClass.MyObjects();

am i better off just having a file for each class? when is it advisable to nest classes? if ever...
 
jon ninpoja
Ranch Hand
Posts: 184
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
duh...its not the object i should be using...but the method...sorry...worked it out
 
jon ninpoja
Ranch Hand
Posts: 184
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...no...that doesnt wor either...im following a tutorial on inner classes...looks the same,but mine isnt working
 
Henry Wong
author
Marshal
Pie
Posts: 22092
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:
why doesnt this work?
ObjectReferenceVariables outerClass = new ObjectReferenceVariables();
        outerClass.MyObjects();

Try ... Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic