Accesing static variable from another class in java

前端 未结 2 1610
遥遥无期
遥遥无期 2020-12-19 10:43

I had a queue implemented as linked list in my multithreaded server. I want to access this queue from another class. Both classes are in the same package. I tried making thi

2条回答
  •  一生所求
    2020-12-19 11:17

    You can access a public static member of another class directly, using the notation ClassName.memberName:

    public class Foo {
        public static String bar = "hi there";
    }
    
    public class Thing {
        public static void main(String[] args) {
            System.out.println(Foo.bar); // "hi there"
       }
    }
    

    public static data members are usually not a great idea (unless they've final), but if you need one, that's how you do it.

提交回复
热议问题