Showing posts with label Static. Show all posts
Showing posts with label Static. Show all posts

Saturday, September 20, 2014

Published 9/20/2014 by with 0 comment

Illegal modifier for parameter ... only final is permitted

A local variable, i.e., method level variable or a parameter cannot be declared as static or public/private/protected.

So local variables must be declared without these modifiers.

For example, the following code is wrong:

    void methodA(static int param) {

        System.out.println(param);
    }

    void methodB(private int param) {

        System.out.println(param);
    }

    void methodC() {

        private static int methodVariable = 10;
        System.out.println(methodVariable);

    }


To resolve the error, remove static and the access modifiers from the local variable declarations. Only the class level variables (declared outside of any method) can be declared with static and access modifiers.
Read More
    email this       edit

Monday, May 9, 2011

Published 5/09/2011 by with 1 comment

modifier static not allowed here


Java error "modifier static not allowed here" can occur for any of the following reasons:

1. Constructor is declared as static(!)

A constructor cannot be static. In the wrong code below:

public class Person
{
private String firstName;
private String lastName;
public static Person()
{
//...
}
}

Here the constructor Person is declared as static. As the constructor must be non-static, the correct code is:

public class Person
{
private String firstName;
private String lastName;
public Person()
{
//...
}
}



2. Interface contains static method(!)

All methods of any interface is abstract and hence they must be non static. See the code below:

public interface MyInterface
{
public static void myMetod();
}

The above code is wrong as the interface method is declared as static. Corrected code is:

public interface MyInterface
{
public void myMetod();
}
Read More
    email this       edit
Published 5/09/2011 by with 1 comment

illegal combination of modifiers: abstract and static

An abstract method cannot be static because the abstract method must be overridden i.e, body of the abstract method must be defined before invocation.

See the wrong code below:

public abstract class Employee
{
public static abstract double earnings(); // error
}

Here the abstract method earnings() is declared as static. An abstract method must be non-static. So the correct code is:


public abstract class Employee
{
public abstract double earnings(); // no error now
}


Read More
    email this       edit