Showing posts with label Method. Show all posts
Showing posts with label Method. 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

Wednesday, June 11, 2014

Published 6/11/2014 by with 1 comment

This method requires a body instead of a semicolon

    email this       edit

Monday, May 9, 2011

Published 5/09/2011 by with 1 comment

interface methods cannot have body

Java error "interface methods cannot have body" occurs when body of the method within an interface is defined.

See the code below:

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

Here the method myMethod has body starting at the symbol { and ending at }. An interface can have abstract methods (method without any body). The class that implements the interface defines the body of the interface methods.

So to remove the error, the method signature/header will end by ; (semi colon) and there will be no body section for the method. Below is the correct code:



public interface MyInterface
{
public void myMetod();
}


Read More
    email this       edit

Thursday, May 5, 2011

Published 5/05/2011 by with 2 comments

invalid method declaration; return type required

The Java error "invalid method declaration; return type required" can occur for any of the following reasons:

1. You are trying to define a constructor (of course without any return type; not even void)  but the constructor name is not given same as the class name.

See the wrong code below:

public class Employee
{
 private int id;
 private String name;
 public employee(int id,String name) // constructor name is not same as the class name.
 {
  this.id = id;
  this.name = name;
 }
}


So check the spelling of the constructor name and also note that Java is a case sensitive language.

2. If you are really defining a method (not a constructor) you have forgotten to declare the return type as the wrong code given below:


public class Test
{
public aMethod() // no return type given
{
System.out.println("This is a method");
}
}

The correct code is:


public class Test
{
public void aMethod() // return type void is given now
{
System.out.println("This is a method");
}
}


Read More
    email this       edit

Sunday, May 17, 2009

Published 5/17/2009 by with 1 comment

non-static method ... cannot be referenced from a static context

If you try to access a non static member of a class from a static context, you will see ‘non-static method … cannot be referenced from a static context’. For example, you might have tried to call a non static method from a static method. Bear in mind, you can access a static member (variables, methods etc) from both static and non-static context but cannot access non-static member from a static context.

See the following example code:

public class NonStaticMethodCannotBeReferencedFromAStaticContext
{
private double firstNumber;
private double secondNumber;
private double result;

public void setFirstNumber(double firstNubmer)
{
this.firstNumber=firstNumber;
}

public void setSecondNubmer(double secondNumber)
{
this.secondNumber=secondNumber;
}

public double add()
{
return firstNumber+secondNumber;
}

public double substract()
{
return firstNumber-secondNumber;
}


public static void main(String args[])
{
setFirstNumber(10.35);// this is a non-static method but referenced from static main method
setSecondNubmer(20.97);
System.out.println("The addition is "+add());
}

}

For the solution you can:
i) declare the non-static member as static and access them from the static context (not always; be careful, this may create other problems)
ii) Create an instance of the class where the non-static members are and then reference the non-static members by the created instance or object.

Browse http://javatech-stuff.blogspot.com/2007/11/differences-between-java-terms.html for understanding the differences between static and non-static member.

Now see the solution below:

public class NonStaticMethodCannotBeReferencedFromAStaticContextSolved
{
private double firstNumber;
private double secondNumber;
private double result;

public void setFirstNumber(double firstNubmer)
{
this.firstNumber=firstNumber;
}

public void setSecondNubmer(double secondNumber)
{
this.secondNumber=secondNumber;
}

public double add()
{
return firstNumber+secondNumber;
}

public double substract()
{
return firstNumber-secondNumber;
}


public static void main(String args[])
{
NonStaticMethodCannotBeReferencedFromAStaticContextSolved object=new NonStaticMethodCannotBeReferencedFromAStaticContextSolved();
object.setFirstNumber(10.35);// accessed by the instance named object
object.setSecondNubmer(20.97);
System.out.println("The addition is "+object.add());
}

}
Read More
    email this       edit
Published 5/17/2009 by with 2 comments

identifier expected

This error message is shown when the statements are not written in proper place. Most often, mistakenly, we may write the processing task (for example assigning value to a variable; writing a loop etc.) outside of any method. In this situation we may see this type of error message.

Then the solution is to write such statements inside the appropriate methods.

See the example code below:
public class IdentifierExptected
{
int number1,number1,sum;


number1=10; // identifier expected
number2=20; // identifier expected
sum=number1+number2; // identifier expected

/*
The assignment statement must be written inside a method. Variables can also be assigned/ initialized during the declaration.
*/


public void display()
{
System.out.println("Number 1 = "+number1);
System.out.println("Number 2 = "+number2);
System.out.println("Sum = "+sum);
}


}

See the solution now:

public class IdentifierExptectedSolved
{
int number1=10,number2=20,sum;


public void sum()
{
sum=number1+number2;
}


public void display()
{
System.out.println("Number 1 = "+number1);
System.out.println("Number 2 = "+number2);
System.out.println("Sum = "+sum);
}



}
Read More
    email this       edit

Tuesday, November 18, 2008

Published 11/18/2008 by with 0 comment

Exception in thread "main" java.lang.NoSuchMethodError: main

'Exception in thread "main" java.lang.NoSuchMethodError: main' is a run time error message. If the main method is not properly defined, we may see this error message. You know if you want to execute/run a program that program must have a main method of the following signature:

public static void main(String args[])

Generally we do the following mistakes in this regard:

1. May be, you haven't defined the main method at all. Define it at first then run it.

2. The method name is not correctly written. The spelling will be exactly in lower case 'main', if we write 'Main' - it will be an error.

For example:

public class NoSuchMethodErrorMain
{
public static void Main(String args) // M is capital here
{
System.out.println("Java is an Object Oriented Language");
}

}




3. The keyword 'static' is omitted. The main method must be static.

For example,

public class NoSuchMethodErrorMain
{
public void main(String args) // keyword static is missing
{
System.out.println("Java is an Object Oriented Language");
}

}



4. The parameter is not declared. Java main method must have a String array parameter. Sometimes, we forget to put [] after the data type or the parameter name (args, here).

For example:

public class NoSuchMethodErrorMain
{
public static void main() // parameter problem
{
System.out.println("Java is an Object Oriented Language");
}

}


OR

public class NoSuchMethodErrorMain
{
public static void main(String args) // the parameter is not array
{
System.out.println("Java is an Object Oriented Language");
}

}


The correct code will be:

public class NoSuchMethodErrorMainSolution
{
public static void main(String args[])
{
System.out.println("Java is an Object Oriented Language");
}

}
Read More
    email this       edit
Published 11/18/2008 by with 0 comment

is already defined

If we declare a variable in a method more than once than we see ' is already defined'. Find out the variable for which the message is shown and check whether you have declared the variable more than one time in a particular block.

If we compile the following code we will see 'country is already defined in myMethod()'
public class AlreadyDefinedSolution
{
public void myMethod()
{
String country;
String country="Bangladesh";// declared again
System.out.println(country);

}

}


Now the solution is to remove the declaration 'String' from the assignment statement.

public class AlreadyDefinedSolution
{
public void myMethod()
{
String country;
country="Bangladesh";//declaration removed
System.out.println(country);

}

}


Read More
    email this       edit

Tuesday, November 4, 2008

Published 11/04/2008 by with 0 comment

missing method body, or declare abstract

If we put a ; (semicolon) after the method header (i.e., before starting the method body) when we are defining a concrete method (which method has body) then we will see “missing method body, or declare abstract”. Another reason is if we don’t write the keyword ‘abstract’ for an abstract method (a method without body), we’ll also see this message.
To resolve the problem we must remove the ; (semicolon) from the header of the concrete method or if it is an abstract method the keyword ‘abstract’ must be written in the method header.

Here is an example of this error:
public abstract class MissingMethodBodyOrDeclareAbstract
{
public void display(String message);
{
System.out.println(message);
}
public void display();
}

Here, a ; (semicolon) is given after the method header of the first display method (with parameter). And for the second display method, keyword ‘abstract’ is not stated in the header and it doesn’t have any body also. Here it is to be noted that if a class contains an abstract method, the class must also be abstract.

The correct code is:

public abstract class MissingMethodBodyOrDeclareAbstractSolution
{
public void display(String message)
{
System.out.println(message);
}
public abstract void display();
}
Read More
    email this       edit
Published 11/04/2008 by with 0 comment

cannot find symbol / cannot resolve symbol

“cannot find symbol” or “cannot resolve symbol” is the most common error message which the programmers get from the compiler. However this error message may mean that the variable/method/class/object used are not declared/defined properly. In most cases, this happens because:

· The variable/object is not declared or the spelling of variable/object declaration & later use of that variable/object is not same.

· The method is not defined or the spelling of the method definition and method calling is not same or the number and/or type of parameters are wrongly given when calling the method or the method where it is defined is not imported properly (in case of class library methods, the package where the method is defined is not imported)

· If the message is shown for a class, the package of that class might not have been imported or the spelling is wrong.

The solution of that problem is to check the spelling at first, Java programmers see this message most often just because of the wrong spelling( be careful about the case also, as Java is a case sensitive language). If the spelling is correct check whether the required package is imported.

Here is an example of this error :

public class CannotFindSymbol
{
public static void main(String args[])
{
int quantity;
Scanner input=new Scanner(System.in);
System.out.print(“Enter the quantity: ”);
quntity=imput.nextInteger();
System.out.println(“Your number is ”+quantity);
}
}


In the above code, there are many lines where we will get this error message.

At first it will say Scanner cannot be resolved/found because the package of this class java.util is not imported.

Also problem in ‘quntity’ as the variable is declared as ‘quantity’

Error in ‘imput’ as the object is created as ‘input’

If you correct all the errors it will show error message for nextIntger as the actual method name is nextInt in class Scanner

Now the correct code is:

import java.util.*;
public class CannotFindSymbolSolution
{
public static void main(String args[])
{
int quantity;
Scanner input=new Scanner(System.in);
System.out.print("Enter the quantity: ");
quantity=input.nextInt();
System.out.println("Your number is "+quantity);
}
}
Read More
    email this       edit