what is the difference between instance variable and methods and their counterparts,clas variable and methods?
i understood that instance is other name for object but what i have undersood till know is that method is somewhat like function (but i could not understand the explaination given for this question in the book i am reading for self study). i am afraid of java plz help to clear my concepts

Recommended Answers

All 4 Replies

Member Avatar for coil

I'll use the example of a Person class:

Instance variable - Variables that are unique to each class e.g. name, age
Class variable - Variables that are common among a class e.g. species
Method - Totally different. A subroutine or function in C languages where code is performed

Instance variable/method - Variables/methods that are specific to each instance (object) of the class e.g. name, age.... When accessing/calling them, the object, as the reference has to be used as a handle.
Class variable/method - Variables/methods that represent/deal with class wide situation, e.g. a static variable which belongs to a class and has only one value specific to the class. When accessing/calling them, the class name is needed as a handle.

The story from text book “Java How to program” by H.M.Deitel & P.J.Deitel, 5th ediction, 2003. p.372) .
“Let us motivate the need for static class-wide data with a video game example. Suppose that we have a video game with Martians and other space creatures. Each Martian tends to be brave and willing to attack other space creature when the Martian is aware that there are at least four other Martians present. If fewer than five Martians are present, each Martian becomes cowardly, so each Martian needs to know the martianCount. …“
Accordingly I have written the following java program to demostrate the usage of static and instance variable/method.

class Martian  {
private String 	name; // instance variable
private static int count; // class variable
 
public Martian(String s){ // constructor with one argument: the name of the Martian
   name = s; 
   System.out.println( name + " was created");
   count++;
	};

/* The method cowardly() is defined as static since it descripts the psychosis of the whole class Martian/all Martians
 * Also be noticed that the static method accesses the static variable(count) only */
 
public static void cowardly() { // the method to test whether each individual Marian is cowardly or not.
System.out.println( // if the total number of Martians is less than 5, they are cowardly.
(count < 5 ? "Martians are cowardly." : "Martians aren't cowardly."));
   }

public static int getCount() { return count; }

protected void finalize(){ // A martian died, the memory has been returned to os
System.out.println("The Martian " + name + " has died.");
count--;
   }
} 

public class MartianTest {

static String name[] = {"Fuzzy", "Sneezy", "Bleary", "Sleazy"}; // names of Martians

public static void main(String args[]) { 
int i;
Martian a[];
a = new Martian[4];

System.out.println(Martian.getCount());

for ( i = 0; i < 4; i++) 
    a[i]= new Martian(name[i]);

	Martian.cowardly();
	Martian b = new Martian("Happy") ; // the Martian named Happy is born.
	Martian.cowardly();
	b=null; // to make the Martian Happy to die.
	System.gc(); 
	Martian.cowardly();

for ( i = 0; i < 4; i++){
		a[i]=null; // to make other 4 Martians to die
		System.gc();
	}

System.out.println(Martian.getCount()); 
/* All Martians dead. The number of Martian should be zero. However, it in fact is not allways true (it is 4 in my case) because the System.gc(), hence the finalize() isn't allways called before  the last line of code is executed. The Thread's priority of the System.gc() is lower than that of this program. So line code: "System.out.println(Martian.getCount());"  could be executed before calling the finalize() method in my mashine. Therefore the number is 4 which shows the situation before calling finalize(). 
Please make comments on my wording.*/
	}
}

output:

0
Fuzzy was created
Sneezy was created
Bleary was created
Sleazy was created
Martians are cowardly.
Happy was created
Martians aren't cowardly.
Martians aren't cowardly.
The Martian Happy has died.
4
The Martian Sleazy has died.
The Martian Bleary has died.
The Martian Sneezy has died.
The Martian Fuzzy has died.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.