Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Lorraine Stephens   on Sep 18 In Java Category.

  
Question Answered By: Sumitra 2004   on Sep 18

Class variables are also known as "static variables." They have the same
value among all instances of a class...considered "shared" variables.
Instance variables belong to specific instances of a class. "Objects"
are what we call instances of a class. Look at the example  below. The
variables and methods marked "static" belong to all Martians. The others
are unique to each individual Martian (alice and marvin).

class Martian {
private static int martianCount = 0;

private String name;
public Martian(String martianName) {
martianCount++;
name = martianName;
}

public static int getMartianCount() {
return martianCount;
}
}

Martian alice = new Martian("Alice D. Martian");
Martian marvin = new Martian("Marvin T. Martian");

Share: 

 

This Question has 5 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on what is instance variable and class variable. Or get search suggestion and latest updates.


Tagged: