简体   繁体   中英

"cannot find symbol" error when invoking a method using reference of class

I feel as if this is super simple but I cant get this to work. I am trying to use this:

import java.util.Scanner;
import java.lang.Math;
public class SammysRentalPriceWithMethods
{

public static void main(String[] args)
{
Rental rental = new Rental();
SammysRentalPriceWithMethods SRPWM = new SammysRentalPriceWithMethods();
getLogo();
getContractNum();
getHoursAndMinutes();
}

public static void getLogo()
{
rental.setlogo();
}

public static void getContractNum()
{
rental.setContractNumber();
}

public static void getHoursAndMinutes()
{
rental.setHoursAndMinutes();
}

}

to call this class and the methods contained inside:

import java.util.Scanner;
import java.lang.Math;
public class Rental
{
public final int minutes = 60;
public final double hourlyRate = 40.0;
private static String contractNum;
private static double hours;
private static int minutes2;
private static double price;
Scanner Input = new Scanner(System.in);

public static void setlogo()
{
System.out.println("*********************************");
System.out.println("*Sammy's makes it fun in the sun*");
System.out.println("*********************************");
}

public static void setContractNumber()
{
Scanner Input = new Scanner(System.in);
System.out.println("Please enter your contract number.");
contractNum = Input.nextLine();
}

public static void setHoursAndMinutes()
{
int timeOver;
Scanner Input2 = new Scanner(System.in);
System.out.println("Please enter the amount of time in minutes you rented the equipment.");
minutes2 = Input2.nextInt();
if (minutes2 > 60)
{hours = (minutes2/60);
price = (hours * 40);
timeOver = (minutes2%60);
price = (price + timeOver);
System.out.println("You rented the equipment for " + hours + " hours and " + timeOver + " minutes.");
System.out.println("Your total price is: " + price);
}
else if (minutes2 < 60)
{
price = (minutes2 * 1);
System.out.println(price);
}
}
}

but the compiler is saying "error: cannot find symbol" on every reference of rental in the SRPWM class. I already called the class in the main method. Any ideas?

The compiler is right.

The scope of the variables rental and SRPWM is restricted to the main method. Either you pass the attributes to the methods of the class or you make them static fields of SammysRentalPriceWithMethods.

The problem is you are mixing both static and non- static members and invoking them inside your SammysRentalPriceWithMethods class, so change the class as shown in the below code with comments:

public class SammysRentalPriceWithMethods {

    private Rental rental;

    //Use constructor to inject Rental object
    public SammysRentalPriceWithMethods(Rental rental) {
        this.rental = rental;
    }

    public static void main(String[] args) {
        //create Rental object
        Rental rental = new Rental();

        SammysRentalPriceWithMethods srpwm =new SammysRentalPriceWithMethods();

        //invoke methods using srpwm reference
        srpwm.getLogo();
        srpwm.getContractNum();
        srpwm.getHoursAndMinutes();
    }

    public void getLogo() {
        rental.setlogo();
    }

    public void getContractNum() {
                rental.setContractNumber();
    }

    public void getHoursAndMinutes() {
                rental.setHoursAndMinutes();
    }
}

You need to remember the following basics:

(1) Call static members of a class using classname and . operator (if you want to call static members within static methods you call them without classname and . )

(2) Call non-static members using the object and . operator

(3) Name the variables, method names with camel case (starting letter in lower case)

Because rental is declared in your main method, it will only be visible in that method. You should consider declaring this variable at the class level.

You need to declare the Rental class variable outside the main() function. If you declare it inside the main() , then you won't be able to use it in other functions. So, make your Rental variable global.

New file should be this:

import java.util.Scanner;
import java.lang.Math;

public class SammysRentalPriceWithMethods {

Rental rental = new Rental();

public static void main(String[] args) { 
SammysRentalPriceWithMethods SRPWM = new SammysRentalPriceWithMethods();

getLogo(); 
getContractNum();
getHoursAndMinutes(); 
}

 public static void getLogo() { 
     rental.setlogo(); 
 } 

 public static void getContractNum() { 
     rental.setContractNumber();
 } 

public static void getHoursAndMinutes() { 
    rental.setHoursAndMinutes();
} 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM