I have create one java project which has following class with it's body.
package tfimvalidation;
public class ValidateToken {
public void display()
{
System.out.println("Yor package imprort succesfully");
}
}
This is java project now I have create jar file of this project and add it in other my dynamic web project.
There is I can access ValidateToken class and package with following statement
ValidateToken validateToken = new ValidateToken();
but I cannot access validateToken.display();
it's give this type of error; Syntax error on token "display", Identifier expected after this token.
This is code of second project where I have use jar of first project. import tfimvalidation.ValidateToken;
public class Main
{
ValidateToken validateToken=new ValidateToken();
validateToken.display(); //Here gives above shown error.
}
The reason you get compile time error because , You are calling token.display();
in the class body
, and not inside a method or other code block
. You can't do that. The least possible change would be:
Shift below statement :
ValidateToken token = new ValidateToken();
token.display();
Into a method like this ,
public static void main(String[] args) {
ValidateToken token = new ValidateToken();
token.display();
}
Other Options
1) Init Block
{
ValidateToken token = new ValidateToken();
token.display();
}
2) Inside Constructor
Main(){
ValidateToken token = new ValidateToken();
token.display();
}
3) Static Block
static {
ValidateToken token = new ValidateToken();
token.display();
}
When you put these statement other than your main method
than either you need to create new Object so that Init Block or Constructor will run
and if it is inside the static block
, it will be called as soon as Class Loads into Memory , but I think you want to reuse the Object for further process also so I suggest you to keep these lines inside your main Method