简体   繁体   English

在方法中使用类变量

[英]Using class variables in methods

In java how to use class variables in methods? 在Java中如何在方法中使用类变量?

this is the code that I have 这是我的代码

public class ExamQ3a {
    String[] descriptionArr = new String[50];
    static int[] codeArr = new int[50];

    public static void displayItems(int count, int[] codeArr,
            String[] descriptionArr) {
        count = this.codeArr.length;
        for (int i = codeArr.length; i < codeArr.length; i--) {

        }
    }
}

The line that is being highlighted here is the count = this.codeArr.length; 这里突出显示的行是count = this.codeArr.length; the error that I am getting is that the non-static variables cannot be referenced from a static context. 我得到的错误是不能从静态上下文引用非静态变量。 But I already made the variable static. 但是我已经将变量设为静态。 So what gives? 那有什么呢?

So as per request only! 因此,仅根据要求! not that I want to ask the whole question, just to know why I want to use static, this is a practice question 不是我想问整个问题,只是想知道为什么我要使用静态,这是一个实践问题

You are to develop a simple application system to manage the inventory in a company. 您将开发一个简单的应用程序系统来管理公司中的库存。 The system should be able to maintain a list of up to 50 items. 该系统应能够维护最多50个项目的列表。 Each item has a unique integer code and a description. 每个项目都有唯一的整数代码和说明。

(a) Write Java statements that declare and create two arrays to store the code and the description of the items. (a)编写Java语句,以声明并创建两个数组来存储代码和项目描述。

(b) Write Java method with the following method signature: (b)用以下方法签名编写Java方法:

public static void displayItems(int count, int[] codeArr, String[] descriptionArr) 公共静态无效的displayItems(int count,int [] codeArr,String [] descriptionArr)

This method displays the code and description of all items in the company in tabular form with appropriate column heading. 此方法以表格形式显示公司中所有项目的代码和描述,并带有适当的列标题。

Parameters: codeArr: the array that stores the codes of the items 参数:codeArr:存储项目代码的数组

descriptionArr: the array that stores the descriptions of the items descriptionArr:存储项目描述的数组

count: the number of items in the system count:系统中的项目数

There is no this in the static world. 在静态世界中没有this Get rid of it. 摆脱它。 To explain, this refers to the current instance , and when you're dealing with static methods or variables, you're dealing with items associated with the class, not with any one particular instance. 为了解释, this是指当前实例 ,当您处理静态方法或变量时,您正在处理与类关联的项目,而不是任何一个特定实例。 So change the code to: 因此,将代码更改为:

count = codeArr.length;

Edit 1 编辑1
As an aside, you don't want to bunch up your closing braces like } } } which makes your code very difficult to read and follow. 顺便说一句,您不想像} } }这样捆起大括号,这会使您的代码很难阅读和遵循。 White space is free, so might as well use it judiciously to improve code readability. 空格是免费的,因此最好还是明智地使用它来提高代码的可读性。

Edit 2 编辑2
You state: 您声明:

so how would I reference the array codeArr to the class variable codeArr? 那么如何将数组codeArr引用到类变量codeArr?

You're inside of the class, and there's no need to use the class variable name here since it is assumed to be used. 您在类的内部,并且在这里无需使用类变量名,因为它被假定为已使用。 Just use the static variable or method name and you should be golden. 只需使用静态变量或方法名称,您应该会很高兴。

Edit 3 编辑3
Your use of static for this type of variable gives the code a bad smell. 您对此类变量使用static会使代码产生难闻的气味。 I'm thinking that your entire program would be much better off if this were an instance variable and not a static variable. 我认为,如果这是一个实例变量而不是静态变量,那么整个程序会更好。 For more discussion on this, you may tell us why you decided to make the variable static. 要对此进行更多讨论,您可以告诉我们为什么决定将变量设为静态。

Is you're going to reference a static variable having the same name as a method parameter you prefix the static variable with the name of the class. 您是否要引用一个与方法参数同名的静态变量,并为该静态变量加上类的名称。 In this case it would be ExamQ3a.codeArr . 在这种情况下,它将是ExamQ3a.codeArr

The other way to handle this is to pick different names for your method parameters, or start using a common prefix for instance/static variables. 处理此问题的另一种方法是为方法参数选择不同的名称,或者开始为实例/静态变量使用通用前缀。

Another point to note is that, in the following piece of code statement1 will never be executed: 还要注意的一点是,在下面的代码段中, statement1将永远不会执行:

for (int i = codeArr.length; i < codeArr.length; i--) { statement1; }

it should be either 应该是

int length = codeArr.length;
for (int i = 0; i < length; i++) { ... }

or 要么

int length = codeArr.length;
for (int i = (length-1); i > -1 ; i--) { ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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