简体   繁体   中英

Using class variables in methods

In java how to use class variables in methods?

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; 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. 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.

(b) Write Java method with the following method signature:

public static void 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

descriptionArr: the array that stores the descriptions of the items

count: the number of items in the system

There is no this in the static world. 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. So change the code to:

count = codeArr.length;

Edit 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
You state:

so how would I reference the array codeArr to the class variable 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
Your use of static for this type of variable gives the code a bad smell. 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 .

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:

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--) { ... }

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