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 916505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:03:50+00:00 2026-05-15T18:03:50+00:00

How do I make fields accessible across a package? Currently, even if they are

  • 0

How do I make fields accessible across a package? Currently, even if they are declared public i’m not able to access the fields from another class in the same package.

  • 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-05-15T18:03:50+00:00Added an answer on May 15, 2026 at 6:03 pm

    Here’s a more apposite example. (Based on @Andreas_D’s example … as if it ain’t obvious!)

    Note that an import is not needed to refer to a class declared in the same package.

    package pkg;
    
    public class One {
      public int fieldOne;
    }
    

    package pkg;
    
    public class Two {
      private One one;
      public int getOne() {
        return one.fieldOne;
      }
    }
    

    Having said this, referring to public attributes declared in another class is generally a bad idea. It is better to declare the attributes as private and access them via getter and setter methods. (If the attributes are exposed as public and they are not final, that anything can modify them. Furthermore, there is no way that a subclass can hide the attributes, or change the way that they behave.)

    EDIT

    To illustrate the last point:

    public class A {
       public int counter;
       private int counter2;
       public int getCounter2() { return counter2; }
       public void setCounter2(int c) { counter2 = c; }
    }
    
    public class B extends A {
       public void setCounter2(int c) {
          if (c < 0) throw new IllegalArgumentException("counter2 cannot be negative");
          super.setCounter2(c); 
       }
    }
    

    Compare the way that the counter and counter2 attributes behave. In the case of counter, any code in any class can read or update the attribute without restriction. This applies both for instances of the class A and any classes that extend A. If some other class decides to set counter to a bad value … there’s nothing to prevent it.

    In the case of counter2, the attribute of an instance of A can only be read or updated by the getCounter2 and setCounter2 methods respectively. If I want to, I can change these methods in A to do validation, perform access checks, notify some listener of a state change an so on. And because this behavior can only occur in methods of A, I know where to start looking in the event of something strange happening.

    If I define a subclass of A (e.g. B), I can override either or both of the methods to do something different. But note that since I decided to declare the actual attribute as private, the setCounter2 method in B still has to call the method in its superclass. And that’s good too.

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

Sidebar

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.