• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating new object

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic