0

I have followed the java SE tutorial from www.patrickvideos.com and have come across a problem in the chapter about "class & object". The person making the tutorial gave the following code example:

Employee alex;
Employee linda;
Employee john;

He said explicitly that "alex, linda, john are what you call objects". However, if i'm not mistaken variables themselves can't be objects but simply hold references to objects or to an instance of a class. So are alex, linda, john objects or is it safe to assume that they are references to objects and that person is spreading misinformation?

syclone
  • 99
  • 13
  • 4
    You have the right intuition. The tutorial is just oversimplifying the problem. Stick with what you know; there's nothing wrong in what you said – Silvio Mayolo Oct 05 '21 at 23:16
  • 2
    But also be aware of the fact that this is a simplification that, for practical purposes, _everyone will make_ because for practical purposes, those variables "are" objects when used. At the technical level, you're right, and at the practical level, that's being needlessly pedantic, so keep that knowledge in your back pocket, but you're both right: they're variables (at the parser level), and they're objects (at the "what does your code treat them as" level). – Mike 'Pomax' Kamermans Oct 05 '21 at 23:53
  • On the other hand, novice Java programmers get confused by these (over-)simplifications ... as amply illustrated by other questions that we get on StackOverflow. From the perspective of such people, this pedantry *in training material* is **necessary**. – Stephen C Oct 06 '21 at 00:19
  • @Stephen C Technically speaking the ability to differentiate between reference variables and the misperception of referring to variables as object isn't pedantry and should be well understood. – Ineversurrender93 Oct 06 '21 at 10:59
  • @Ineversurrender93 - That is **exactly** my point! Which is why it is so important for the training material to be (pedantically) correct. And why the OP is correct to call this out. Hopefully someone brings this to the attention of the folks at PatrickVideos! – Stephen C Oct 06 '21 at 11:51

3 Answers3

5

Yes, reference variables

You are correct, alex, linda, and john are all reference variables.

All three are little containers that hold either nothing (null) or a reference (pointer) to an object setting in memory somewhere else. Those three variables do not hold an object, but they know how to get you to an object.

Given the syntax in Java, we tend to forget about this distinction. In our daily programming work, we usually think of alex as the object. We generally think:

alex holds a Person object representing the human known as Alex

… whereas the technical truth is:

alex may hold a reference that can lead us to the chunk of memory elsewhere holding our Person object that we want to access

In a tutorial, it can be tiring and distracting to constantly refer to alex and linda as reference variables. So perhaps we should give the author of your tutorial some poetic license to call them objects.

Example code

Some example code, using the records feature in Java 16+.

record Person( String firstName , String lastName , Color favoriteColor ) { }

Person alex = new Person( "Alex" , "Peterson" , Color.MAGENTA );
Person linda = new Person( "Linda" , "Greystone" , Color.GREEN );
Person john = new Person( "John" , "Petrov" , Color.YELLOW );

Each call to new Person instantiates an object, that is, allocates a chunk of memory somewhere in RAM to hold our three member fields (state) along with handles to the executable code we think of as named methods (behavior). (For records, the compiler implicitly creates constructor, getters, equals & hashCode, and toString methods. Plus, you can add more.)

The call to new Person returns a reference, essentially the address in memory where the new Person object/instance lives. We store that reference in the reference variable alex or linda. We declared those variables as being references to hold objects of type Person.

diagram of reference variables pointing to objects

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Yes ... but programmers are not poets ... as we tell people who waste too much of their or the bosses time "beautifying" their code :-) – Stephen C Oct 05 '21 at 23:50
  • And unfortunately many novice Java programmers start out very confused about the distinction between variables and objects because "poetic" tutorials blur the boundaries with statements like the one that the OP found. Someone writing / presenting training material for novice programmers should be clear and accurate. His poetic license should be revoked :-) – Stephen C Oct 05 '21 at 23:56
  • Nice illustration! – Arvind Kumar Avinash Oct 08 '21 at 10:26
2

You are right that, strictly speaking, in the above example, we are declaring variables alex, linda, john that will hold reference to an object.

But since they hold reference to an object, in a juggling of terminologies, we can call them objects.

So a variable can be considered a syntactic construct, while the object (what it refers to) is its semantic.

It's like in Math, you can say sin(x) is a function, but strictly speaking it is an expression. To be a function you also have to specify its domain, so sin(x) in this case is refering to the function x -> sin(x) where x is a real number.

Gnut
  • 541
  • 1
  • 5
  • 19
0
  • Employee is a class
  • alex is an object of the class Employee
  • alex is an instance of the class Employee
  • alex for the jvm is the variable of object that doesn't contains the object data.
  • alex is said to be an instance of Employee and an Employee object for simplicity.

In Java, no variable can ever hold an object. A variable can only hold a reference to an object [1].

Instead of holding an object itself, a variable holds the information necessary to find the object in memory. This information is called a reference or pointer to the object [1].

Object is not stored in the variable.The object is somewhere else; the variable points to it [1].

[1] https://math.hws.edu/javanotes/c5/s1.html

Mahmoud Aly
  • 578
  • 3
  • 10