• 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

Referencing an Object's Fields

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To access a field, you can use a named reference to an object, as in the previous examples, or you can use any expression that returns an object reference. Recall that the new operator returns a reference to an object. So you could use the value returned from new to access a new object's fields:



This statement creates a new Rectangle object and immediately gets its height. In essence, the statement calculates the default height of a Rectangle. Note that after this statement has been executed, the program no longer has a reference to the created Rectangle, because the program never stored the reference anywhere. The object is unreferenced, and its resources are free to be recycled by the Java Virtual Machine.

Source: http://docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html

I really don't understand this:
1. How the above statement is grammatically correct ? How it is working ?
2. What does it mean by 'default height' (means the purpose of this code) ?



Suppose Rectangle returns:
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rasul Patrick wrote:
1. How the above statement is grammatically correct ?



What do you think is wrong with it?

How it is working ?



Do you understand how this code works?


It's the same thing, except we're not storing a reference to the Rectangle object in a variable. The expression new Rectangle() is of the type "reference to Rectangle". The expression rect is also of the type "reference to Rectangle." When we say height = <SOMETHING>.height all that matters is that <SOMETHING> is of the type "reference to Rectangle". It doesn't matter if <SOMETHING> is a variable name or the result of the new operator or the result of a method call, such as int height = getSomeRectangle().height;

(And actually, it doesn't even have to be Rectangle. It just has to be some type that has an accessible height field.)


2. What does it mean by 'default height' (means the purpose of this code) ?



It means the height of a newly created Rectangle if we don't specify the height explicitly as a constructor parameter and haven't set it after construction.

The purpose of this particular code is to demonstrate how you can use this kind of construct and what its results are. The purpose of this kind of code in general is to be lazy and not create an extra variable when you don't need one. Some people will say that's bad practice. I kind of agree with them, but I do it myself occasionally too.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You ought not to do that. You ought to have the inner workings of the Rectangle class marked private, and accessible via getXXX methods, so you should say
System.out.println(rect.getHeight());
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You ought not to do that. You ought to have the inner workings of the Rectangle class marked private, and accessible via getXXX methods, so you should say
System.out.println(rect.getHeight());



True.

I assume he's talking about this though, so it may not be up to him.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic