Reputation: 594
I have been coding in Java for about a week now but I am still having issues learning it.
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,
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()
.
And the return type is 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
?
What is it ? shouldn't it be this.data
?
Upvotes: 0
Views: 1633
Reputation: 21718
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:
int x = 2;
Object y = new Object().
Upvotes: 1
Reputation: 21004
They probably created the object static
because they want it to be global
. For example, anywhere in the code you will be able to call 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..
If it would simply be XMLGettersSetters data = null;
instead of static...
then it could not be accessed from anywhere in the code.
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.
Upvotes: 2
Reputation: 33
The object is instantiated, but then placed into a static variable. This means that you always access the same instance of the 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.
Upvotes: 0