Accollativo
Accollativo

Reputation: 1559

Static method in java, can I access in a non-static way to a static method?

I think that there's something wrong in my code, is it the same to call (in setIstance) the method isSameMonth() in the static way or in a non static way? The compiler suggest me to change: timesheet.isSameMonth() to Timesheet.isSameMonth()

I think no, because I want to pass the local variable timesheet, is it the same thing or should I modify my code?

Timesheet class:

static private Timesheet timesheet;

static public Timesheet getIstance()
{
    if (timesheet == null || !Timesheet.isSameMonth())
    {
        timesheet = null;
        timesheet = new Timesheet();
    }
    return timesheet;
}

static public void setIstance(Timesheet timesheet)
{
    if (timesheet != null && timesheet.isSameMonth())
    {
        Timesheet.timesheet = timesheet;
    }
}

public static boolean isSameMonth()
{
    Date now = new Date();
    Calendar calendarNow = Calendar.getInstance();
    calendarNow.setTime( now );
    Date firstDay = timesheet.days[0];
    Calendar calendarFirstDay = Calendar.getInstance();
    calendarFirstDay.setTime( firstDay );
    if (calendarNow.get(Calendar.MONTH) == calendarFirstDay.get(Calendar.MONTH))
    {
        return true;
    }
    return false;
}

From outside I make this call:

Gson gson = new Gson();
String json = sharedPrefs.getString("timesheet", "");
if (!json.isEmpty())
{
    try
    {
        Timesheet timesheet = Timesheet.getIstance();
        if (timesheet.getDay(0)==null)
        {
            Timesheet.setIstance( gson.fromJson(json, Timesheet.class) );
        }
        refreshWidget(timesheet, context, allWidgetIds, intent.getAction());
    }
    catch (Exception e)
    {
        Log.e(TAG_LOG, e.toString());
    }
}

Upvotes: 0

Views: 446

Answers (4)

Deltharis
Deltharis

Reputation: 2373

The fact that you ask that question should make you think about the design. I see no reason why isSameMonth should be a static method if every usage is connected with the preserved instance.

Not strictly an answer to the question in the topic, but apparently it helped

Upvotes: 1

icza
icza

Reputation: 417512

timesheet is not a local variable, it is a static field of the class. And your IDE suggests you to change timesheet.isSameMonth() to Timesheet.isSameMonth() because that method is static and it's better to access it in that (static) way.

If timesheet would not be static, you would have already get another compile error saying that the static isSameMonth() cannot access the non-static variable.

Accessing a static method works both way: via the Class name and via an instance reference but the same code will be generated. You can even access the static method even if the reference is null:

Runtime r = null;
r.getRuntime(); // This works even though r is null

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

No. There is no need change your code other than the method calling style Timesheet.isSameMonth().

Since that is a static method, the style of invoking the method by convenience is with Class name, rather than the instance variable.

Otherwise, the people reading your code might thinks that it is a instance method. That is why it is a suggestion by IDE to make everyone life easy.

Upvotes: 1

Eran
Eran

Reputation: 393781

It works either way, but the convention is to refer to static methods via the class name :

Timesheet.isSameMonth()

This makes it clear to someone who reads your code that you are calling a static method.

Upvotes: 2

Related Questions