• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the job of instance variables in java?  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Came across this exercise to use a map instance variable and made me realize, I have no idea what instance variables actually do and what their job is. What purpose do they serve? why do we need them? I have a vague understanding that when we create a class declaration line, right under that we set up our class with static and instance variables, so this tells what our class "HAS" like if we are creating a Person class:

private String name;
private int age;

I vaguely understand that what they do. I know when we create an object, we are gathering up all the stuff that it means to be that class, so all those attributes listed and methods too. The object gets all that stuff that was defined, provided there is a constructor for it, right? not sure of that is right.

For an example I could write:



I still don't get what the job of instance variables are especially in a more vague and less real-life sort of use of instance variables for example:

I found a project online that asks for these specifics and it confused me because my understanding instance variables is not there.

The task was:

// make a class with this Map instance variable
// This member maps a student’s name (a String) to a list of courses (also Strings) that he/she is taking.
public Map<String, ArrayList<String>> courses;


// then write out the following methods:
// (1). Constructor, which instantiates the (empty) map
public Database() {}


// (2). An add method Add this course for this student.
// with two case to handle. If the student is not in the system yet
// and if the student is in the system we add to their existing course list.
public void add(String student, String course) {}


// (3). Remove this course from this student’s list.
// If the student is not in the system, or if this course is not
// on their list, the method should do nothing.
public void remove(String student, String course) {}


This task confused me because it uses instance variables and my understanding of them is very fuzzy.
Please would be so grateful if someone could explain in detail what instance variables do/their job and also explain their role in a project like this.

Thank you
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is an object oriented programming language.

This means that your Java program will consist of classes, and from those classes you create objects. A class is a blueprint to make objects out of.

Objects hold data in member variables and have behaviour which works on that data - this behaviour is implemented in the methods in the class of the object.
That idea is the core of object oriented programming.

Look at your class Person. A Person object holds to pieces of data: the name of the person and the age of the person. This data is stored in the member variables 'name' and 'age'.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you gone through the official tutorial that explains all this?  It's here: https://docs.oracle.com/javase/tutorial/java/concepts/object.html
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Java is an object oriented programming language.

This means that your Java program will consist of classes, and from those classes you create objects. A class is a blueprint to make objects out of.

Objects hold data in member variables and have behaviour which works on that data - this behaviour is implemented in the methods in the class of the object.
That idea is the core of object oriented programming.

Look at your class Person. A Person object holds to pieces of data: the name of the person and the age of the person. This data is stored in the member variables 'name' and 'age'.


Thanks for answering! it's very appreciated

If I am understanding correctly, we create instance variables to store the STATE of the object at some specific moment in time, but instance variables are always changing, right? age could be 14 and then a method runs and now the age is 18 for some reason, and maybe the person's name is "Bob" but a method is called and it's change to "Fred" and any number of changes to some particular object can be made, and that's the point, right? that we want to have these amphibious orbs that at first come in a GENERALIZED cookie cutter state and then we start messing around and customizing them.

But behaviors (methods) those never change unless we specifically go in there and make changes to them. But I am thinking usually we first make some solid well-rounded methods to carry out our task and routines and then we start instantiate objects over and over again in the main method. So we sort of replicate the same object and then start specializing them by applying different methods to their instance variables.

So instance variables HOLD onto what the CURRENT state is at a particular moment in time for the program, which often will change depending on what the at hand is.

However, seems like sometimes we just create instance variables to do some task not so much for the object for example. Like something more arbitrary and less real-life, like sorting algorithms, where is the concept of a person object and his/her's state and behavior. Should I not always think of an object in terms of like a Person or a Car? when we start getting into things like linear, binary search and selection, bubble, and quick sort those ideas of Person and Car object begin to start getting hazy in my mind, things are more abstract and less like a real life example.


Why is it useful to store the state of an object at a particular time in the program?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:. . . If I am understanding correctly, we create instance variables to store the STATE of the object . . .
Yes.
but instance variables are always changing, right? . . .
Not necessarily. Just because they are called variable doesn't mean they have to change. It is quite possible to have fields which don't change. Consider a car; its speed changes from moment to moment, but its make doesn't change.
. . .  behaviors (methods) those never change unless we specifically go in there and make changes to them. But I am thinking usually we first make some solid well-rounded methods to carry out our task and routines
The difference is that you have to change the source code to change methods but fields' values can be changed at runtime.
and then we start instantiate objects over and over again in the main method. So we sort of replicate the same object and then start specializing them by applying different methods to their instance variables.
No. You don't replicate objects. Each object is a different entity from all other objects. They might have the same structure but not the same identity.
Once an object is created, you don't specialise it. You may alter its state, however, but that is not specialisation.
. . . sometimes we just create instance variables to do some task not so much for the object for example. Like something more arbitrary and less real-life, like sorting algorithms . . .
No, an algorithm is not an object nor a field. It is something you do and can be implemented by a method/methods.
Why is it useful to store the state of an object at a particular time in the program?
Depends on the program. You may need to find a previous value.

Objects encapsulate data and behaviour. Their data live in the fields and their behaviour lives in the methods.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:
However, seems like sometimes we just create instance variables to do some task not so much for the object for example. Like something more arbitrary and less real-life, like sorting algorithms, where is the concept of a person object and his/her's state and behavior. Should I not always think of an object in terms of like a Person or a Car? when we start getting into things like linear, binary search and selection, bubble, and quick sort those ideas of Person and Car object begin to start getting hazy in my mind, things are more abstract and less like a real life example.

Why is it useful to store the state of an object at a particular time in the program?

Most of this has to do with abstraction and self-containment (encapsulation).  If you see an instance variable that has nothing to do with the state of a specific object, then you need to question the decision to represent that value as an instance variable. Perhaps it's better as a local variable or a parameter or a return value that is used by the caller.  You will find many examples that do not exemplify the proper use of instance variables so you need to learn to tell the difference between proper uses and improper uses.

Regarding thinking about objects in terms of a Person or Car, that's a good place to start. The danger is that you fall into a trap of always trying to find a real-world equivalent for your classes. In many cases, there isn't.  A class/object is simply an abstraction that helps you think about a set of behaviors and related data that can influence those behaviors as a single "entity".  For example, there is no real-world equivalent of an Iterator or Comparator or an EventListener. These things are simply abstract ideas.  Abstraction involves simplification and delegation. When you use an Iterator, you're saying "I don't really care how you go through the collection, I want you to take care of those details for me (delegation). Just give me the next item in the collection every time I call your next() method (simplification). On my part, I agree to check hasNext() to make sure that you are able to return something if I call next() again."  The set of public methods that you can call is the API or contract that you have with the object. All the other details are hidden from you because you don't really need to know.

If you take it back to the real world, the steering wheel of a car is an abstraction.  There's a "contract" for its use that you can intuitively understand: When you turn the wheel to the left, the car will go left. If you turn it right, the car will go right. If you "straighten out" the wheel, the car will go straight forward. You don't care about everything that goes on between that thing that you're holding and manipulating and the actual wheels of the car. Most people can't even tell you if they have a rack and pinion or a recirculating ball or just a bunch of ropes and pulleys under the hood. That's abstraction and information hiding.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!