• 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 Creating Objects (Syntax Question)

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good morning, everyone:

I am doing my mental memory exercises again, and I had a question (concept) about creating a object and it's syntax. When you create an object as

myClassType objClass = new myClassType();

is the same as

objClass = new myClassType();

Is the question.

Thanks.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"myClassType objClass = new myClassType();" does three things...

It creates a reference variable called 'objClass' that can point to an object of type "myClassType" or any of it's sub-classes.

It creates an object of type myClassType.

It assigns the address of the object created to the reference variable.




objClass = new myClassType();

only does the last two of those things. Someone, somewhere, had to have created the reference already. If not, you'll get a compilation error.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as creating the object goes, it's the same if the variable "objClass" has been declared already somewhere else.
 
Preston Thornton
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay thanks,

So I just want to run through the numbers.

For the case of the second < ObjClass = new myClassType(); >

. . .
myClassType ObjClass

. . .
ObjectClass = new myClassType(); Right?!?

Okay, I think I have it. I am starting to heavily review my java basics for the 1st Java Sun Exam.

Thanks again.
Preston
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming that's a typo, sure.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic