Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding reference variable

 
kiruthigha rajan
Ranch Hand
Posts: 69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello i am new to this forum.can anyone please explain the elucidated stuff given below with an example

A reference variable can be of only one type, and once declared, that type
can never be changed (although the object it references can change).

A reference is a variable, so it can be reassigned to other objects, (unless the
reference is declared final).

A reference variable's type determines the methods that can be invoked on
the object the variable is referencing.

A reference variable can refer to any object of the same type as the declared
reference, or—this is the big one—it can refer to any subtype of the
declared type!

thanks in advance
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go through the tutorial - http://docs.oracle.com/javase/tutorial/java/index.html

After reading the tutorial check if this program helps you...



And read - HowToAskQuestionsOnJavaRanch
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kiruthigha rajan wrote:hello i am new to this forum.can anyone please explain the elucidated stuff given below with an example

A reference variable can be of only one type, and once declared, that type
can never be changed (although the object it references can change).


As its statically and strongly typed language- the type of the variable is determined at compile time and cannot be changed during runtime. And Strongly typed because each variable should have a type associated.

kiruthigha rajan wrote:
A reference is a variable, so it can be reassigned to other objects, (unless the
reference is declared final).


Lets consider a sample class-


So you create an instance of Animal-


And then you can assign animal1 to some other instance of Animal


But if its declared as final-


kiruthigha rajan wrote:
A reference variable's type determines the methods that can be invoked on
the object the variable is referencing.

Consider the Animal class, the methods you can invoke on an instance of animal class are those present in the Animal class and those present in the super class which Animal extends (provided the Super class has them declared as either public or protected)
By default in Java all classes extend the Object class. Check out the Javadoc for the methods present in Object class.


kiruthigha rajan wrote:
A reference variable can refer to any object of the same type as the declared
reference, or—this is the big one—it can refer to any subtype of the
declared type!

Consider another class-


}
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Welcome to JavaRanch!
You need to go through some tutorials, work out some code to understand the concepts. Just trying to understand what they mean from the language point of view, you can get confused.
The best way to learn a programming language is to WRITE CODE.
 
kiruthigha rajan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:and Welcome to JavaRanch!
You need to go through some tutorials, work out some code to understand the concepts. Just trying to understand what they mean from the language point of view, you can get confused.
The best way to learn a programming language is to WRITE CODE.


awesome explanation..thanks alot
 
kiruthigha rajan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,i am unable to understand the question itself can anyone help me with an example

Which methods can be invoked when the PlayerPiece object is being referred
to using a reference declared as type Animatable? Only the animate() method.

the code is:
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question is if you have a reference type of the Animatable interface which methods can you call with that variable.
 
Jason Bullers
Ranch Hand
Posts: 111
8
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:Question is if you have a reference type of the Animatable interface which methods can you call with that variable.

In code (just for some added clarity), that would be:

 
kiruthigha rajan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai everyone.. i am unable to understand the concept given below?how can a reference variable access a static method.as they have told before that static members=class and non static=instance.
then how cum is this possible.if possible please explain with an example.thanks in advance

"But just to make it really confusing, the Java language also allows you to use an
object reference variable to access a static member:
Frog f = new Frog();
int frogs = f.frogCount; // Access static variable
// FrogCount using f"


the whole program is given below:

class Frog {
static int frogCount = 0; // Declare and initialize
// static variable
public Frog() {
frogCount += 1; // Modify the value in the constructor
}
}
class TestFrog {
public static void main (String [] args) {
new Frog();
new Frog();
new Frog();
System.out.print("frogCount:"+Frog.frogCount); //Access
// static variable
}
}
 
Jason Bullers
Ranch Hand
Posts: 111
8
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declaring a field as static means it exists only once, in the class. The alternative is instance variables, which exist in each and every instance of the class that gets created. So, if you have an instance variable (let's call it 'i') defined in your class (let's call it 'C'), then every object of type C can have its own value for i. If you have a static variable (let's call it 's') defined in your class, then every object of type C shares the value of s (i.e. they would all see the same value, and if you changed it in one place, every existing instance would see the new value).

Generally, you want to access static members through the class itself (for example, C.s), but, because those static members are accessible by every instance, you can access them through the instance as well. There may be a slightly more technical reason as to why this is the case, and I don't know it. One thing to remember though: just because you can access static members through the instance, doesn't mean you should; prefer to use the class (Frog.frogCount in your example).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic