• 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

When to use "new" when creating a new instance of an object or variable?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have noticed that sometimes "new" is omitted when creating a new variable. Specifically, I have seen this when creating a new int or String. What are the rules regulating when you have to use "new" or just declare a variable? For example:
 
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should get you started with Java. Instantiating primitives and objects
 
J Steele
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the resources - exactly what I was looking for!
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"new" is not relevant to the creation of a variable. It is used to create an instance of a class, also called an "object." It returns a reference to the newly created object, which may be stored in a variable of a type matching that of the object's class. In Line 1, you have created a variable that is of type int, which is a primitive, not a class. Anything that returns an integer may be stored in that variable. In Line 2, you have created a variable of type String. A reference to any String object may be stored in that variable. In Line 3, you have created a variable of type MyClass. A reference to any MyClass object may be stored in that variable. As it happens, in Line 3, you are also creating a new object of type MyClass. Well, you're not doing that, because you haven't completed the syntax necessary to invoke a constructor. Most likely, it would look like this:

There is no way, without knowing the class's constructor signature(s) to be sure, however, if the parenteses should be empty, or enclose an argument list. However, once a new object has been created, its reference (returned by new) can be stored in any variable of type MyClass.

Lines 3 could be rewritten thus:


Again, that's because variables are created independently of any objects, and new is used to create new objects.
 
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic