This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

When Creating Objects (Syntax Question)  RSS feed

 
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.
 
lowercase baba
Bartender
Posts: 12613
50
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.
 
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
 
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that's a typo, sure.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!