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

  • SEARCH
  • Home
  • 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 8749495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:42:15+00:00 2026-06-13T12:42:15+00:00

I have 3 classes : – Tell – The main program Item – The

  • 0

I have 3 classes : –

Tell – The main program
Item – The individual telephone directory items
Directory – A directory object which stores all the items.


What I’m trying to do is create an array list within directory which stores the objects from the item class and this is how I’m doing it.

From Tell, I’m invoking method as: –

Directory.add(name, telNo);

Directory Class: –

public class Directory
{
    ArrayList<Entry> entries = new ArrayList<Entry>();
    // Add an entry to theDirectory
    public static void add(String name, String telNo)      
    {
         entries.add(new Entry(name, telNo));
    }
}

Entry Class: –

public class Entry
{
    String name;
    String telNo;
    public TelEntry(String aName, String aTelNo )
    {
        setNumber(aTelNo);
        setName(aName);
    }

    private void setNumber(String aTelNo)
    {
        telNo = aTelNo;
    }
    private void setName(String aName)
    {
        name = aName;
    }

}

However my program does not compile, and it displays this error: –

"non-static variable entries cannot be referenced from a static context" 

Can anyone explain what I’m doing wrong?

  • 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-13T12:42:15+00:00Added an answer on June 13, 2026 at 12:42 pm

    You need to declare your ArrayList in Directory class as static since you are using it from a static context – your add method. And also you can make it private, as your fields should be private, and provide a public accessor method to access it.

    private static ArrayList<Entry> entries = new ArrayList<Entry>();
    

    You can only access static variables from a static context. Because, non-static variables need an instance of your class to be used, and there is no instance available in a static context, so you cannot use them.

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

Sidebar

Related Questions

In my program I have classes for Vertex's Edges and Faces which I'll hopefully
I have classes which have automatic properties only like public customerName {get; set;}. They
I have classes which previously had massive methods so I subdivided the work of
I have classes with many properties, which consists, in some cases, other complex classes.
I have classes which are inherited from abstract Packet( this class has abstract method
I have implemented Customer, DVD, Receipt, classes and 2 files for main functions and
I have classes such as AccountsController, ProductsController etc that all inherit from BaseController. Unity
I have classes which get private properties set via the constructor. I would then
Does the Swing API have classes which combine InputVerifiers? For example, I have three
My original question was quite incorrect, I have classes (not POJO ), which have

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.