• Post Reply Bookmark Topic Watch Topic
  • New Topic

When Creating Objects (Syntax Question)  RSS feed

 
Preston Thornton
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • 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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that's a typo, sure.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!