• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Reference Variable

 
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All -

So, I'm watching a very interesting YouTube video and he brings up Reference Variable.  It seems to be a variable from another Class in another Package.








I have been researching but I"m still not clear on its use or function in the code snippet above (please see Line 7).

Can anyone help me understand?

Thanks in advance.
 
Saloon Keeper
Posts: 14488
325
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This class declares that a Person has a personName, and that the personName is of the type Name.

It doesn't do much yet, because the variable is never assigned a value other than its default value, so a call to the name() method would always return null.

Presumably in lesson 3 the author will elaborate on the Name class, and maybe add more code to the Person class. I find it very strange that they would introduce a type from lesson 3 in lesson 2 though.
 
Royale Summers
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stephan -

I stopped watching him and I am now seeking enlightenment from other YouTuber's!

Thank you for your time.



 
Royale Summers
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Can someone please explain what is happening in Line 34?  I'm determined to understand this!
 
Saloon Keeper
Posts: 9712
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know what a "reference" is, but what is a "Reference"? Presumably a class that is not part of the Java language distribution.
 
Royale Summers
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey -

Appears to be an object.  What does it mean when there's an object and a variable in the parameter list?
 
Carey Brown
Saloon Keeper
Posts: 9712
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Reference" is a class that specifies the type of the parameter.
 
Royale Summers
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey -

If you are referring to Line 34 then this is where I'm confused.

So, the code is passing both the class Reference as well as a variable named reference?  I'm not clear on what's happening and what values are currently stored?
 
Sheriff
Posts: 17264
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a poor example. Or rather, it’s a good example of poor choice of names.

The Reference name (capital letter “R”) is the name of a class. The reference name (lowercase “r”) is the name of the parameter. It’s no different from something like this:

Here, the object parameter is declared as having the type Object. In the code that’s confusing you, the reference parameter is being declared as having the type Reference. That’s all it is, nothing more complicated.
 
Junilu Lacar
Sheriff
Posts: 17264
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you’re familiar with the public static void main(String[] args) construct, the same concept applies: the parameter args is declared to have the type String[] (a String array).
 
Junilu Lacar
Sheriff
Posts: 17264
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to give some clarity about the concept, a “reference variable” is a variable whose type is a reference type, which is either a class, an interface, an enumeration type (enum), or an array. This is in contrast with a “primitive variable” which is a variable declared as an int, long, double, float, short, byte, or boolean.
 
Royale Summers
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Junilu -

Thank you for your input.


 
Royale Summers
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For anyone reading this thread and wishes to better understand reference variable, please see the following link:


https://www.geeksforgeeks.org/data-types-in-java/

Here's an excerpt:

"The Reference Data Types will contain a memory address of variable value because the reference types won’t store the variable value directly in memory. They are strings, objects, arrays, etc.

String: Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’.
Below is the basic syntax for declaring a string in Java programming language."
Staff note (Junilu Lacar) :

Do some research: search for are Java strings terminated with '\0'? and you'll find many articles that definitively say NO, Java Strings are NOT terminated by '\0'

Staff note (Campbell Ritchie) :

One of those articles saying Java® Strings aren't char[]s is in the JLS

 
Junilu Lacar
Sheriff
Posts: 17264
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Royale Summers wrote:String: Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’.


I am 99% positive that this is wrong.
 
Royale Summers
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Junilu -

That's something that you would have to take up with the author.  I'm glad I found this forum for both clarification and confirmation.


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

Royale Summers wrote:. . . String: Strings are defined as an array of characters. . . .

The Java® Language Specification (=JLS) specifically contradicts that, presumably because earlier Java® users came from C/C++ where you do get zero‑terminated char[]s to use as Strings.
 
Junilu Lacar
Sheriff
Posts: 17264
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Royale Summers wrote:. . . String: Strings are defined as an array of characters. . . .

The Java® Language Specification (=JLS) specifically contradicts that, presumably because earlier Java® users came from C/C++ where you do get zero‑terminated char[]s to use as Strings.


Thanks for that clarification, Campbell.

I had found some places with statements claiming that whether or not a NUL character was used to terminate String was an implementation detail, that it was up to the implementer of that particular Java compiler/JVM. Those claims are trumped by the JLS which, of course, is the final authoritative source of truth on all things Java.
 
Junilu Lacar
Sheriff
Posts: 17264
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing to note: relying on “facts” like “Strings are NUL-terminated” is not something you want to do in your Java programs or even any program in general.

As much as possible, avoid relying on any particular implementation detail. Robust programs rely on intent as represented by the APIs that are available for use, not implementation. Relying on an implementation is dangerous and in general, the risk of having the rug getting pulled out from under you if/when the implementation changes far outweighs any short-term benefits you may gain from leveraging that implementation detail.
 
Campbell Ritchie
Marshal
Posts: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . I had found some places with statements claiming that whether or not a NUL character was used to terminate String was an implementation detail . . .

Yes, the existence of a \0/NUL would be an implementation detail, but the JLS section I quoted specifically denies that there is a terminal \0/NUL. If you did that, you would have to allocate additional space; the String "Junilu" would have a length of 7. That is normal to C programmers, who have to allocate one extra space for the \0/NUL. Have another look at the JLS, where it mentions the String#toCharArray() method.If you allow \0/NULs, which of those two lengths will be 6 and which 7? Or both the same? If you don't terminate Strings with \0/NUL, both those figures will predictably come to 6.
And what if (‍) you want a \0/NUL in the middle of a String? The no‑\0/NUL implementation will allow that, if anybody feels daft enough to want to do that. This JLS section mentions U+0000 and thereby specifically permits \0/NULs as parts of Strings. If you tried that in C, you would truncate the String at that point.

Please tell us where you found those quotes about \0/NUL, so we can avoid them. There are unfortunately lots and lots of bad tutorials on the Net.
 
Junilu Lacar
Sheriff
Posts: 17264
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My previous example showed a class (static) variable that was also a symbolic constant (declared as final). Not all class variables are constants though.

For example, you might want to track the total number of instances that have been created for the Dog class. (Don’t take this as a recommendation for good practice — it’s just an example to illustrate a concept)


Assuming that’s the only constructor for the Dog class, then the population class variable will tell you how many times the Dog() constructor has been invoked.

You might notice that line 1 declares the Dog class to be final, meaning it cannot be extended by a subclass. That has more to do with design than anything directly related to class vs. instance variables. I gave a caveat earlier that this example is not to be taken as a recommendation for good practice because if there are other constructors or if you extend the Dog class, then you’d have to remember to update the population variable in every constructor. That’s a problematic design at best.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All -

Interesting discussion although I was focusing on the Reference variable.

I did learn some new things and that's always a good thing.

Thanks for your contributions.
 
Campbell Ritchie
Marshal
Posts: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Royal Summers wrote:All - . . . Thanks for your contributions.

That's a pleasure

Royal/Royale: how many of you are there?
 
Junilu Lacar
Sheriff
Posts: 17264
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to make sure you’re not inadvertently misled here...

A class (static) variable is shared by all instances of the same class. In this sense, there's only one variable.

However, you can declare as many class (static) variables as needed, and they don’t all have to be constants either.


As you can see, there are a number of class (static) variables declared here. As I said, you can declare as many static variables in a class as you want or as makes sense. All instances of class Dog will share these values, meaning their values are not specific to any particular instance, unlike instance variables which are.

Note that what I showed with the population field is only an example to demonstrate the concept, not to demonstrate a recommended design approach. This kind of design can actually be problematic, which is why I felt compelled to make the Dog class in the example final. If you have other constructors, you'll have to remember to increment the population variable in each one as well. If this class could be extended, then all subclasses would possibly have to remember to update that field as well. That's not a very good design.

EDIT: Not sure what's going on but I thought for sure my original reply that showed the use of a population static variable had somehow disappeared. Does this have anything to do with the Royal vs Royale double accounts?
 
Campbell Ritchie
Marshal
Posts: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . . This kind of design can actually be problematic . . .

Another problem with that particular design is that there is no straightforward way to know when an object goes out of use so you can't say population--; anywhere so as to get the population right.
 
What's that smell? Hey, sniff this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic