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 202 views
2 answers 154 views
1 answer 193 views
193 views asked Jul 18, 2014 by avibootz
1 answer 117 views
1 answer 107 views
107 views asked Feb 3, 2024 by avibootz
1 answer 191 views
...