Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Overriding static variable of parent class?

I have the following class which I'm using as the base of all the models in my project:

public abstract class BaseModel {     static String table;     static String idField = "id";             public static boolean exists(long id) throws Exception     {         Db db = Util.getDb();         Query q = db.query();         q.select( idField ).whereLong(idField, id).limit(1).get(table);          return q.hasResults();     }      //snip.. } 

I'm then trying to extend from it, in the following way:

public class User extends BaseModel {     static String table = "user";     //snip } 

However, if I try to do the following:

if ( User.exists( 4 ) )    //do something 

Then, rather than the query: "SELECT id FROM user WHERE id = ?", it is producing the query: "SELECT id from null WHERE id = ?". So, the overriding of the table field in the User class doesn't seem to be having any effect.

How do I overcome this? If I added a setTable() method to BaseModel, and called setTable() in the constructor of User, then will the new value of table be available to all methods of the User class as well?

like image 812
Ali Avatar asked Oct 18 '13 18:10

Ali


People also ask

Can I override static variable in Java?

Static methods are bonded at compile time using static binding. Therefore, we cannot override static methods in Java.

Why static variables Cannot be overridden?

No, we cannot override static methods because method overriding is based on dynamic binding at runtime and the static methods are bonded using static binding at compile time. So, we cannot override static methods.

Can you override instance or static variables?

3) An instance method cannot override a static method, and a static method cannot hide an instance method.

Can you override a static method?

In short, a static method can be overloaded, but can not be overridden in Java. If you declare, another static method with same signature in derived class than the static method of superclass will be hidden, and any call to that static method in subclass will go to static method declared in that class itself.


1 Answers

You cannot override static methods or fields of any type in Java.

public class User extends BaseModel {     static String table = "user";     //snip } 

This creates a new field User#table that just happens to have the same name as BaseModel#table. Most IDEs will warn you about that.

If you change the value of the field in BaseModel, it will apply to all other model classes as well.

One way is to have the base methods generic

protected static boolean exists(String table, long id) throws Exception {     Db db = Util.getDb();     Query q = db.query();     q.select( idField ).whereLong(idField, id).limit(1).get(table);      return q.hasResults(); } 

and use it in the subclass

public static boolean exists(long id) {     return exists("user", id); } 

If you want to use the field approach, you have to create a BaseDAO class and have a UserDAO (one for each model class) that sets the field accordingly. Then you create singleton instances of all the daos.

like image 91
Cephalopod Avatar answered Sep 22 '22 15:09

Cephalopod