View RSS Feed

My Java Tips

Using static and final attributes – An example

Rate this Entry
by , 11-21-2011 at 04:47 PM (4205 Views)
In this post, I will present an example which used static and final keywords with attributes in meaningful way. I hope after going through this, you will develop good understanding of these.


As may know, that we may access static member of class without using any object of that class. Final field/attribute means that you cannot change its value. If you try to change the value of final attribute, you will get an error – some this like this:


Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The final field Student.MAXCOUNT cannot be assigned

Ok – so basics are clear. Not coming to the point. I want to write a class that has some limitations. I want to track the number of object of this class. So a class variable will be there which will be incremented on each object creation. What better place to increment this variable other than constructor. Since, this variable should remain the same for all the instances of the class, therefore we should declare it as static.

I also want an upper limit for the objects. I don’t want more than 10 objects of my class to be created. Static variable again will be a good choice. But since, no one should be allowed to change this from the code, we may declare it as static and final.



Time for an example. I want a limitation on number of object that can be created of my classes called Student.


Java Code:
public class Student {

private int id;
private String name;
private static int counter;
private static final int MAXCOUNT=10;

public Student(int id, String name) throws Exception {

counter++;
if(counter > MAXCOUNT)
throw new Exception("Limit exceeded. Object not created");
this.id = id;
this.name = name;
System.out.println("Object created - " + counter);

}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public void showAll()
{
System.out.println("id: " + id);
System.out.println("name: " + name);

}
}
In the constructor, we increment the counter. Also we check if we have reached the upper limit or not. If yes, we throw an exception.

Now lets make sure if this works:


for(int i=0;i<20;i++)
{
Student obj3 = new Student(i, "name");
}

Output:


Object created - 1
Object created - 2
Object created - 3
Object created - 4
Object created - 5
Object created - 6
Object created - 7
Object created - 8
Object created - 9
Object created - 10
Exception in thread "main" java.lang.Exception: Limit exceeded. Object not created
at Student.(Student.java:13)
at Test1.main(Test1.java:16)

10 objects were created as shown in the output. When we tried to create 11th object, exception was thrown.

So it works J

Happy coding.

Submit "Using static and final attributes – An example" to Facebook Submit "Using static and final attributes – An example" to Digg Submit "Using static and final attributes – An example" to del.icio.us Submit "Using static and final attributes – An example" to StumbleUpon Submit "Using static and final attributes – An example" to Google

Comments