user4174871
user4174871

Reputation: 65

Using .jar files in the main function

Once I import .jar file to the main function. I have done everything correctly : (Right clickproject-­‐>Properties-­‐>JavaBuildPath-­‐>Libraries-­‐>AddJARs).

Now if I want to start using methods and variables from a class that's in the .jar file.

In the main function, do I just declare an object of the class type and start using methods from the class?

I tried that and it keeps giving me the error: "Syntax error on token ";", @ expected"

package abcd;

import util.Car;

public class Showroom {

   Car firstCar = new Car("BMW"); //overloaded constructor 

   Car secondCar = new Car(); //default constructor is empty 

   secondCar.setName("Lexus"); //setName is a method in the Car class but I get the above error for using this. 

}

I know it has a very easy solution but can you please explain what and why?

Upvotes: 0

Views: 67

Answers (2)

SparkOn
SparkOn

Reputation: 8946

In java you can write logics only in some method or blocks.

so something like this

public void someMethod(Car secondCar){
secondCar.setName("Lexus");
}

Upvotes: 2

Jigar Joshi
Jigar Joshi

Reputation: 240900

you need to write statements in executable block (constructor, method, initializer block)

secondCar.setName("Lexus");

Upvotes: 1

Related Questions