Answers for "java function static variable"

2

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
    }
}
Posted by: Guest on February-04-2022
0

java static variable

Person notReallyAPerson = null;
notReallyAPerson.qtdone++; // this works!
Posted by: Guest on March-15-2022

Code answers related to "java function static variable"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language