• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Custom class type variable

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I'm trying to understand the line public Juice liquid; in Glass.java class. The Juice in the line is a type and liquid is variable right?

And In Guest.java we reference g.liquid to a Juice object. In here I think g is the only variable and the liquid is another variable returned by Glass class right?

As far as I've understood, liquid is a Juice type variable which means it can't hold Int, float, String or anything else. Does that mean the only use of that variable can be to reference to a different object? Or can is it possible to make liquid hold a int value?

Glass.java


Juice.java


Guest.java







 
Saloon Keeper
Posts: 14511
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Herrington wrote:The Juice in the line is a type and liquid is variable right?


Yes and yes.

In here I think g is the only variable and the liquid is another variable returned by Glass class right?


g is a local variable of type Glass, so it holds a reference to an object of type Glass. The dot is the so called 'member access operator'. After the dot, you specify a simple name and if the name is that of an instance field of the Glass class, it will return the current value of that field for that Glass instance.

Does that mean the only use of that variable can be to reference to a different object? Or can is it possible to make liquid hold a int value?


Variables of a reference type can only hold references to objects of the specified type, or subtypes. A variable of type Glass can not hold an int, but it CAN hold a reference to an instance of GlassVase, which is a piece of glass that can hold a liquid.
 
I am Arthur, King of the Britons. And this is a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic