Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

static int java

// A static variable is shared among all instances of a class
// In example below, Slogan class has a static variable called
// count that was used to count the number of Slogan objects created.
public class Main {
	public static void main(String[] args) {
		Slogan slg1 = new Slogan("Talk is cheap");
		Slogan slg2 = new Slogan("Live free or die");
		System.out.println(Slogan.getCount());}
}
class Slogan {
	private String phrase;
  	// Only one copy of variable count is created
    // And it would be shared among all Slogan objects
	private static int count = 0;
	public Slogan(String phrase) {
		this.phrase = phrase;
		count++;}
	public static int getCount() {
		return count;}
}
 
PREVIOUS NEXT
Tagged: #static #int #java
ADD COMMENT
Topic
Name
2+7 =