Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java static variable

// A static variable is shared among all objects of a class
class Slogan {
	String phrase; // string description of slogan
    static int count; // slogan count    
    public Slogan(String phrase) {
    	this.phrase = phrase;
        count++;
    }
}

public class SloganCounter {
	public static void main(String[] args) {
    	Slogan slg1 = new Slogan("Live free or die!");
        Slogan slg2 = new Slogan("Don't worry, be happy!");
    
    	System.out.println("Slogans count: " + Slogan.count); 
        // Above outputs: Slogans count: 2
    }
}



 
PREVIOUS NEXT
Tagged: #java #static #variable
ADD COMMENT
Topic
Name
4+5 =