简体   繁体   English

实例化一个类的静态对象

[英]Instantiating static object of a Class

I have been coding in Java for about a week now but I am still having issues learning it. 我已经用Java编写了大约一个星期的代码,但是学习它仍然遇到问题。

I know that we can create a class and then create instance of it by using the name of a class. 我知道我们可以创建一个类,然后使用一个类的名称来创建它的实例。

but I have this code which is giving me trouble understanding what is happening here, 但是我有这段代码,这让我很难理解这里发生的事情,

This is the file called XMLGettersSetters.java, 这是名为XMLGettersSetters.java的文件,

public class XMLGettersSetters {

    private ArrayList<String> title = new ArrayList<String>();
    private ArrayList<String> artist = new ArrayList<String>();
    private ArrayList<String> country = new ArrayList<String>();
    private ArrayList<String> company = new ArrayList<String>();
    private ArrayList<String> price = new ArrayList<String>();
    private ArrayList<String> year = new ArrayList<String>();

    public ArrayList<String> getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company.add(company);
        Log.i("This is the company:", company);
    }

    public ArrayList<String> getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price.add(price);
        Log.i("This is the price:", price);
    }

    public ArrayList<String> getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year.add(year);
        Log.i("This is the year:", year);
    }

    public ArrayList<String> getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title.add(title);
        Log.i("This is the title:", title);
    }

    public ArrayList<String> getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist.add(artist);
        Log.i("This is the artist:", artist);
    }

    public ArrayList<String> getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country.add(country);
        Log.i("This is the country:", country);
    }

}

Now I can create object of this class like this, 现在,我可以像这样创建此类的对象,

XMLGettersSetters myObject = new XMLGettersSetters();

but from the website where I am learning this code, they have created the objects like this, 但是在我正在学习此代码的网站上,他们创建了这样的对象,

public static XMLGettersSetters data = null;

How come the object is declared static ? 为什么将对象声明为静态对象? what does the above code even mean. 上面的代码甚至意味着什么。 Shouldn't it just be, 不应该这样

 XMLGettersSetters data = null;

From what I know, when we declare a variable as static then we donot need to instantiate a class to use a static variable from that class. 据我所知,当我们将变量声明为静态变量时,我们无需实例化一个类即可使用该类中的静态变量。

One more question, 还有一个问题,

public static XMLGettersSetters getXMLData() {
    return data;
}

I have no idea what happened in the above code, first the object is instantiated as static then instead of giving object a name, a function is given instead which is getXMLData() . 我不知道上面的代码中发生了什么,首先将对象实例化为静态,然后给对象命名,而不是给函数getXMLData() And the return type is data 返回类型是data

Now about the code below, 现在关于下面的代码,

public static void setXMLData(XMLGettersSetters data) {
    XMLHandler.data = data;
}

A method is created with XMLGettersSetters object as an argument, but what about XMLHandler.data ? 使用XMLGettersSetters对象作为参数创建一个方法,但是XMLHandler.data呢? What is it ? 它是什么 ? shouldn't it be this.data ? 难道不是this.data吗?

They probably created the object static because they want it to be global . 他们可能创建了static对象,因为他们希望它是global For example, anywhere in the code you will be able to call XMLHandler.data . 例如,在代码中的任何位置,您都可以调用XMLHandler.data (I'm supposing here the class in which is created the data variable is XMLHandler because it is used in the setter method.. (我在这里假设在其中创建数据变量的类是XMLHandler因为它在setter方法中使用。

If it would simply be XMLGettersSetters data = null; 如果仅仅是XMLGettersSetters data = null; instead of static... then it could not be accessed from anywhere in the code. 而不是static...则无法从代码中的任何位置访问它。

As for the XMLHandler.data used instead of this.data you have to know that by convention, most of the people specify the class name before the object they are accessing when accessing a static variable. 至于使用XMLHandler.data代替this.data您必须知道,按照惯例,大多数人在访问静态变量时都会在要访问的对象之前指定类名。

Static is a field, not an object. 静态是字段,而不是对象。 Static fields are per class, shared by all code that have access to this field. 静态字段是按类的,由有权访问该字段的所有代码共享。 They are initialized only once, when the class is first loaded. 首次加载类时,它们仅初始化一次。 Usual fields (without static) are per object instance. 常规字段(无静态)是每个对象实例的。 They are initialized when the object instance is created. 它们在创建对象实例时初始化。

In Java, you can assign the value in the same sentence where you declare the variable: 在Java中,可以在声明变量的同一句子中分配值:

 int x = 2; 
 Object y = new Object().

The object is instantiated, but then placed into a static variable. 该对象被实例化,但是随后被放入一个静态变量中。 This means that you always access the same instance of the XMLGettersAndSetters. 这意味着您始终访问XMLGettersAndSetters的相同实例。

As the methods are static you have to refer to a static variable rather than this.data which refers to the variable in the current instance. 由于方法是静态的,因此您必须引用静态变量,而不是this.data引用当前实例中的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM