3

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

2
  • 1
    If you want to have access to static element (field/method) you should use your class name like ExamQ3a.codeArr, but you can skip class name if your code is inside class. Commented Oct 25, 2012 at 2:27
  • @noobprogrammer : ur string array needs to be static in order to be accessed in to the static method. i would suggest u use dynamic array like arraylist , would be more convenient Commented Oct 25, 2012 at 6:50

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

so from your explanation I understand that the codeArr array that I am referencing to is the class variable correct?
regarding the edit1, in my actual code I do use that but when I pasted here I coupled them together cause for easy reading(guess not) , tks
Together with removing the this, he/she should rename the codeArr, that refers to the local parameter variable.
@M.M. I have edited my original post with the question that I am trying to answer
1

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.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.