Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to use static variable in class with Java

2 Answers

0 votes
public class worker {  
    private int id;  
    private String name;
    static String work_place ="mic-roogle-zon-book";  

    worker(int n, String nm){  
        id = n;  
        name = nm;  
    }  
    
    // copy constructor 
    worker(worker w) {  
        id = w.id;  
        name =w.name;  
    }
    
    void print() {
        System.out.println(id + " " + name + " " + work_place);
    }  
   
    public static void main(String args[]) {  
        worker w1 = new worker(876234, "Dan");  
        worker w2 = new worker(w1); 
        
        w1.print();  
        w2.print();  
   }  
}  



/*

run:

876234 Dan mic-roogle-zon-book
876234 Dan mic-roogle-zon-book

*/

 



answered Sep 27, 2019 by avibootz
0 votes
public class Test {
    static int count = 0;
    
    Test() {  
        count++;
        System.out.println(count);  
    }  
    public static void main(String args[]) {
        Test t1 = new Test();  
        Test t2 = new Test();  
        Test t3 = new Test();
        Test t4 = new Test();  
    }
}


/*
run:

1
2
3
4

*/

 



answered Sep 27, 2019 by avibootz

Related questions

1 answer 164 views
2 answers 123 views
1 answer 178 views
178 views asked Jul 18, 2014 by avibootz
1 answer 97 views
1 answer 90 views
90 views asked Feb 3, 2024 by avibootz
1 answer 164 views
...