File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Regarding reference variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regarding reference variable" Watch "Regarding reference variable" New topic
Author

Regarding reference variable

kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69
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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Sep 08, 2007
Posts: 3064
    
  33

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 | My Blog
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

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

Joined: Dec 29, 2011
Posts: 69
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

Joined: Dec 29, 2011
Posts: 69
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
Bartender

Joined: May 31, 2011
Posts: 1776
Question is if you have a reference type of the Animatable interface which methods can you call with that variable.
Jason Bullers
Greenhorn

Joined: Dec 27, 2011
Posts: 28

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

Joined: Dec 29, 2011
Posts: 69
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
Greenhorn

Joined: Dec 27, 2011
Posts: 28

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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding reference variable
 
Similar Threads
will method overloading apply in this example?
Reference varibale question
Reference variable concepts
whats the difference between Objects and reference variables?
question about declare and create an object