Java 尝试调用从另一个类定义变量的方法时发生编译错误

Java 尝试调用从另一个类定义变量的方法时发生编译错误,java,static,Java,Static,javdocs中对这两类进行了深入描述,但简而言之:这两类用于计算电力使用产生的二氧化碳排放量。但是,我在尝试编译时返回一个错误,其内容如下: 无法从静态上下文引用非静态方法CalAverageBill(java.util.ArrayList) 我不确定问题是什么,有人能帮忙吗 public class CO2fromElectricity { private double monthBillAverage; private

javdocs中对这两类进行了深入描述,但简而言之:这两类用于计算电力使用产生的二氧化碳排放量。但是,我在尝试编译时返回一个错误,其内容如下:

无法从静态上下文引用非静态方法
CalAverageBill
java.util.ArrayList

我不确定问题是什么,有人能帮忙吗

public class CO2fromElectricity
{
    private double monthBillAverage;                           
    private double monthPriceAverage;                          
    private double annualCO2emission;                         
    private double emissionFactor;                             
    private double months;                                    

    CO2fromElectricity() { }

    public double calcAverageBill(ArrayList<Double> monthlyBill)
    {
        monthBillAverage = 0;
        for(double billToken : monthlyBill)
        {
            monthBillAverage += billToken;
        }
        return monthBillAverage / monthlyBill.size();
    }


    public double calcAveragePrice(ArrayList<Double> monthlyPrice)
    {
        monthPriceAverage = 0;
        for(double billToken : monthlyPrice)
        {
            monthPriceAverage += billToken;
        }
        return monthPriceAverage / monthlyPrice.size();
    }


    public double calcElectricityCO2(double avgBill, double avgPrice)
    {
        emissionFactor = 1.37;
        months = 12;
        return annualCO2emission = avgBill / avgPrice * emissionFactor * months;
    }
}


public class CO2fromElectricityTester
{
    public static void main(String[] args)
    {

        ArrayList<Double> monthlyBill = new ArrayList<Double>();                                                
        ArrayList<Double> monthlyPrice = new ArrayList<Double>();                                               
        double averageMonthlyBill,                                                                               
               averageMonthlyPrice,                                                                             
               annualCO2emission;                                                                               
        double monthlyBillToken1[] = {192.14, 210.42, 231.25, 186.13},                                           
               monthlyPriceToken1[] = {.07, .06, .08, .06};                                                      

        for(int index = 0; index < monthlyBillToken1.length; index++)                                            
        {
            monthlyBill.add(monthlyBillToken1[index]);                                                           
            monthlyPrice.add(monthlyPriceToken1[index]);                                                        
        }

        ArrayList<CO2FootprintV1> electricCO2outputput = new ArrayList<CO2FootprintV1>();


        averageMonthlyBill = CO2fromElectricity.calcAverageBill(monthlyBill);                                    
        averageMonthlyPrice = CO2fromElectricity.calcAveragePrice(monthlyPrice);                                 
        annualCO2emission = CO2fromElectricity.calcElectricityCO2(averageMonthlyBill, averageMonthlyPrice);      


        System.out.println("Average Monthly Electricity Bill: " + averageMonthlyBill);                           
        System.out.println("Average Monthly Electricity Prince: " + averageMonthlyPrice);                        
        System.out.println("Annual CO2 Emissions from Electricity Useage: " + annualCO2emission + "pounds");     
    }
}
公共级二氧化碳微电
{
私人双月平均工资;
私人双月费;
私人双年展;
私人双重排放因子;
私人双月;
二氧化碳介电性(){}
公共双现金票据(ArrayList monthlyBill)
{
月平均数=0;
用于(双倍票据代币:月票)
{
monthBillAverage+=billToken;
}
返回monthbillage/monthlyBill.size();
}
公共双平均价格(ArrayList monthlyPrice)
{
月电价=0;
用于(双倍票据代币:月费)
{
monthPriceAverage+=billToken;
}
返回monthPriceAverage/monthlyPrice.size();
}
公共双CalCelectricyCO2(双avgBill,双avgPrice)
{
排放系数=1.37;
月=12;
返回年度2排放量=平均租金/平均价格*排放系数*月份;
}
}
公共级二氧化碳微电测试仪
{
公共静态void main(字符串[]args)
{
ArrayList monthlyBill=新建ArrayList();
ArrayList monthlyPrice=新ArrayList();
双倍平均每月账单,
每月平均价格,
每年2次;
双月比利代币1[]={192.14,210.42,231.25,186.13},
月价格指数1[]={.07,06,08,06};
for(int index=0;index
问题在于,
CalAverageBill
是一种对类型为
二氧化碳热电
的对象进行操作的方法,但您将其称为静态方法。也就是说,您必须在
二氧化碳微电
的实例上调用它,例如:

CO2fromElectricity inst = new CO2fromElectricity();
averageMonthlyBill = inst.calcAverageBill(monthlyBill);
或者,您可以通过向方法定义中添加static关键字使方法成为静态的:

public static double calcAverageBill(ArrayList<Double> monthlyBill)
public静态双重平均账单(ArrayList monthlyBill)

快速查看一下
CO2fromElectricity
,您可能希望您定义的字段(例如
monthBillAverage
)在每个方法中都是局部变量,而不是字段或静态属性,因为在实例范围或所有执行范围内定义这些字段对您的类没有好处(事实上,您可能会在当前定义它们的方式中遇到一些麻烦).

记住,对于未来的问题,我们根本不需要所有这些注释。对于你自己的改进:你不需要很多内联注释。
//Main method
之类的东西是毫无意义的。它是类的一部分。我并不希望它出现在代码中,而是我正在学习的课程需要它。你不是暗示我应该去回到我的代码中,在我发布之前编辑掉所有必需的评论?是的,这正是我暗示的。这与问题无关,所以它不应该在那里。你相信我会回到代码中编辑掉所有的评论,因为你个人不喜欢它们,这是非常愚蠢的。我从来没有抱怨过,一个d它们不是问题——它们不会抑制功能,我也不知道你对它们有什么问题。这不仅仅是我的观点:它们是指导原则。问题应该描述具体的问题,而不是用无关紧要的东西把文章弄得乱七八糟。你文章中的评论太多,很难有一个概述。如果你想讨论这个问题(我总是错的),你可以这样做。