• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating new object  RSS feed

 
waleed zedan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void main(String argv[]){
new ClassConstructor();


what does this code means , i tried to invoke a class method , like this
new myClass().myMethod()

the complier couldnt find myMethod

in which cases should i use this syntax ?
thanks
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not entirely certain what you want to do. It looks to me like you've stumbled upon the syntax for creating an object and invoking it all at the same time.

Perhaps you can post all of your code so that we can really see what it is you're trying to do.

I'm moving this to Java in General (Beginner).
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class method is another term for a static method.

If myMethod() is static, you can just say Your version of the code does extra work by creating an object. Since myMethod() is static, the object you created is unreachable and immediately eligible for garbage collection. However, your call to myMethod() will work fine.

If myMethod() is not static, your code creates an object of class myClass and calls myMethod on that object. When myMethod returns, the object will be eligible for garbage collection unless myMethod() saved a copy of this in a static member of myClass or outside of myClass.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!