Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding the "Creating an object" statement  RSS feed

 
Joey Chen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Foo d = new Foo();

I understand that this statement is to create a new Foo object.

1. Is the new object "d" or is "d" a variable?

2. Am I correct to assume "new Foo()" means to create a new Foo object?

3. What is the purpose of the "Foo" in "Foo d"?

I know.. really easy questions..
[ May 14, 2007: Message edited by: Joey Chen ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joey Chen:
Foo d = new Foo();

I understand that this statement is to create a new Foo object.

1. Is the new object "d" or is "d" a variable?

2. Am I correct to assume "new Foo()" means to create a new Foo object?

3. What is the purpose of the "Foo" in "Foo d"?

I know.. really easy questions..

[ May 14, 2007: Message edited by: Joey Chen ]


1. d is not the object. d is a reference to the newly created object.

2. Yes new Foo() creates a new Foo object.

3. Foo in that context identifies what type of reference d is.
 
Joey Chen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Keith Lynn:


3. Foo in that context identifies what type of reference d is.


Thanks for the response.

However, I don't understand why there needs to be a "Foo" in "Foo d" to identify what type of reference d is referring to if "new Foo()" already does that? It seems to me that the "Foo" in "Foo d" is redundant.

Why isn't it just d = new Foo(); ?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joey Chen:


Thanks for the response.

However, I don't understand why there needs to be a "Foo" in "Foo d" to identify what type of reference d is referring to if "new Foo()" already does that? It seems to me that the "Foo" in "Foo d" is redundant.

Why isn't it just d = new Foo(); ?


The type of d doesn't need to be Foo, it just needs to be able to hold a Foo object. The following is also valid Java code:

Object d = new Foo();

Also remember that

d = new Foo();

as valid Java code for using a variable that has been declared earlier. That is, with your proposal you wouldn't be able to see whether this actually is a declaration of a new variable, or the usage of an already existing one.
 
Joey Chen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I understand now. Thank you.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if the Cup Size story helps. It's cute, and pretty profound underneath it all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!