• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Understanding the "Creating an object" statement

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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(); ?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I understand now. Thank you.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See if the Cup Size story helps. It's cute, and pretty profound underneath it all.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic