Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with setting instance variables in a class?  RSS feed

 
Joe Simpson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently going through a Java course and I have a question about what is currently taking place.

I have two classes that I am working with, one called Assignments, and one called Student.

After writing some basic methods within the Assignment Class my professor switched over to the Student class and proceeded to write the following code.

private double mGpa;
private Assignment mCurrentAssignment;


What is going on with the second line? Are you calling the Class Assignment and making it a variable? I am confused with how this is working?
I appreciate any help!

thanks.
 
salvin francis
Bartender
Posts: 1588
35
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi scotty,



is defining a reference to class Assignment. As your teacher may have explained, there is a difference between class and objects. You can define a class and then create many objects from it. Now, to access those created objects, you can use a reference which "points" to that object.
The above statement declares a reference to class Assignment.
You can create an object and assign it to this reference as:

 
Joe Simpson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so I am referring to my Class Assignment. I understand that now. So the mCurrentAssignment is just the name of my object now? After I initialize it within the class that I am referencing Assignment from?
 
salvin francis
Bartender
Posts: 1588
35
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Simpson wrote:So the mCurrentAssignment is just the name of my object now? After I initialize it within the class that I am referencing Assignment from?

Your object is created in memory in java, to access it in code you need some kind of reference/handle so that you can "refer" to it.

Lets see an example...


Output:
My name is Joe
My name is Joe
My name is Salvin
My name is Joe



Let me know if you have any doubts in the above program
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So to sum up, remember, objects do not have names. To access the object you use a reference variable. It is called a reference variable because it "refers" to an object.

In your example, you are simply declaring a reference variable. The name of this reference variable is mCurrentAssignment.

Reference variables also have a type - this specifies the type of object they can refer to.

In your example, you have specified the type as Assignment. This means that the reference variable can only point to objects that are of type Assignment.

Note that no object has yet been created - once you create the Assignment object, you can store a reference to it in the reference variable.
 
Joe Simpson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. That makes sense now! I appreciate the quick responses!
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Simpson wrote:Ok so I am referring to my Class Assignment. I understand that now. So the mCurrentAssignment is just the name of my object now?

Yes, ALL variable declarations take the form:

  <visibility> [static] [final] <type> <name>[ = <value>];

where:
  • <visibility> - is public, private, protected, or default (package-private)), depending on what you want to be able to "see" the variable.
      - and unless you have a very good reason not to, make it 'private'.
  • static - (optional) indicates that the variable belongs to the class, not an object.
  • final - (optional) indicates that the variable can be initialized exactly once - ie, that it's a constant.
  • <type> - indicates the type of the variable (in your case Assignment).
  • <name> - whatever name you want the variable to have - this should start with a lower-case letter.
  • <value> - (optional) is a value you want to assign to the variable immediately.

  • HIH

    Winston
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!