-3

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.
}   
Uday A. Navapara
  • 1,237
  • 5
  • 20
  • 40
  • 1
    You haven't shown the code *calling* display at all. Please provide a short but complete example. – Jon Skeet Feb 07 '15 at 08:22
  • 1
    Please add more of your code to your question, specially surrounding code for `validateToken.display();` call. – Mehraban Feb 07 '15 at 08:22
  • do validateToken.display(); v in lower case – singhakash Feb 07 '15 at 08:24
  • Please be more careful when posting questions in the future. Make sure you include a full example, and make it an example of what's actually wrong - otherwise you waste people's time, as you have here by changing the call from `ValidateToken.display()` to `validateToken.display()` after two of us have already answered. – Jon Skeet Feb 07 '15 at 08:30
  • Sorry that's my mistake but above is my real code now and the method display() is not static so please give solution for this – Uday A. Navapara Feb 07 '15 at 09:49

4 Answers4

3

You can't just call a method in a class declaration like that. You can declare fields in a class declaration, but method calls (other than those used for field initializers) have to be in methods or constructors. For example:

import tfimvalidation.ValidateToken;

public class Test {
    public static void main(String[] args) {
        ValidateToken token = new ValidateToken();
       token.display();
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • This is not static method I have shown in above code – Uday A. Navapara Feb 07 '15 at 08:27
  • 1
    @user3526172: No, so you shouldn't call it as *if* it were a static method, using just the type name. That's my point. You have two problems in your code, and you need to understand them both. – Jon Skeet Feb 07 '15 at 08:27
  • 3
    @user3526172: Oh, and now I see you've changed the question so it *doesn't* call it like that. What's the point of providing code for us and then changing it? – Jon Skeet Feb 07 '15 at 08:29
1

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

Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
0

Call your void type method from main/any other method instead of calling as if declaring a field like:

public class Main
{
    public static void main(String args[]) {//main is the entry point
       ValidateToken validateToken=new ValidateToken();
       validateToken.display();
    }
}   

Or

public class Main
{
    ValidateToken validateToken=new ValidateToken();
    public void myMethod() {
       validateToken.display();
    }
}   

Or

public class Main
{
    ValidateToken validateToken;
    public Main() {
        validateToken=new ValidateToken();
        validateToken.display();
    }

}   

if it were a method with say return type as int, you could have done something like

public class Main
{
    ValidateToken validateToken=new ValidateToken();
    int myint = validateToken.display();//then of course method name would been different
}   
SMA
  • 36,381
  • 8
  • 49
  • 73
  • You need to call void method from another object in another method or constructor. – SMA Feb 07 '15 at 08:28
0

What you are doing

public class Main
{
     ValidateToken validateToken=new ValidateToken();
    ValidateToken.display(); //Here gives above shown error.
}   

what you should do

 public class Main
    {
       //call this function from main
      public void function(){
         ValidateToken validateToken=new ValidateToken();
        validateToken.display(); // here v in lowercase
       }
    }   

First you are calling display function inside class but not inside Main class function thats not allowed in java second your method in jar is non-static so you have to call it after creating its object.

In case you want to call display(); with class name do In you jar

public static void display()
    {
        System.out.println("Yor package imprort succesfully");
    }

In class where you called function of jar

ValidateToken.display();

Note: Also import the package.

singhakash
  • 7,891
  • 6
  • 31
  • 65