Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8742397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:29:35+00:00 2026-06-13T11:29:35+00:00

In java how to use class variables in methods? this is the code that

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:29:36+00:00Added an answer on June 13, 2026 at 11:29 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take this code: public class MyClass { private final Object _lock = new Object();
i m new with netbeans platform , i want to use java class's method
i use java reflections to get methods used in a class.I also want to
I use java 7, and I create a varargs method public class JavaApplicationTest {
I noticed that in Java, you can use a class which has members of
I have a set of source folders. I use a Java class to build
I'm working with java, trying to use the AudioFormat Class. I have the following
Here is some of my Java code public List<OBJ> a = new ArrayList<OBJ>(); public
I have the following code that gets initialized as a static variable in a
I am developping an Android app, and I need to use java Signature class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.