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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:51:59+00:00 2026-06-03T12:51:59+00:00

What I really need is to be able to declare regular variables in an

  • 0

What I really need is to be able to declare regular variables in an interface and implement that interface in two classes that I would not have to have to re-declare these in each class (ie class.data.variables instead of class.variables). Is there any way that I could achieve the same goal differently?

To give more detail. Essentially, I have created a small drawing program that drops JLabels on a JPanel that is on a JScrollPane. Because I have a specific design for these JLabels (ie they are not just for drawing they represent airline objects for this application), I have a class that extends JLabel and adds my application specific variables to it. Ultimately, I read and write an XML file with these variables so they can load and save their designs. Since I can not use this extended class for my XML definitions because it screams about the parent class even though I told it to have NONE as the accessor (I read there is a bug), I have to create an identical class and copy values back and forth for saving and loading. Not too much of a problem except when I add a variable to the JLabel extended class and forget to add it to the XML mimic class and subsequent copy routines.

So, it would be great if I could make one class (say CellDataRecord.java) that held the extra data declarations and have that class be used in both places (the JLabel extension and the XML data) without having to have something like XML.data.CellDataRecordXXX.

  • 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-03T12:52:00+00:00Added an answer on June 3, 2026 at 12:52 pm

    You can do that with inheritance or using an interface, where the variable is set as a constant in the parent class. Since you are extending a JLabel, you should implement the interface on both classes:

    public interface MyInterface {
        int someint = 9;
    }
    
    public class MyClass1 extends JLabel implements MyInterface {
        //this class has access to `someint`
    }
    
    public class MyClass2 extends JLabel implements MyInterface {
        // also has access to `someint`
    }
    

    Edit

    Since you want to be able to change the same variable from different classes, you have to ensure you aren’t changing copies and are changing the same variable, so you should use a volatile keyword on the variable to indicate to java that all threads should check the value before it updates it.

    Now you’ll need to have a separate class so that instances can be made from other classes to get the value. You have to use the static keyword to ensure that one copy is kept for all class instances.

    public class MyVariableWrapper {
        public static volatile int some_var = 9;
        public void updateSomeVar(int newvar) {
             some_var = newvar;
        }
        public int getSomeVar() { return some_var; }
    }
    

    Now the other two classes just do this:

    public class MyClass1 extends JLabel {
        MyVariableWrapper myVariableWrapper;
        MyClass1() {
            super();
            myVariableWrapper = new MyVariableWrapper();
            // now I have access to `some_var`
        }
    }
    
    public class MyClass2 extends JLabel {
        MyVariableWrapper myVariableWrapper;
        MyClass2() {
            super();
            myVariableWrapper = new MyVariableWrapper();
            // now I have access to the same `some_var` as MyClass1
        }
    
        // this is a wrapper method for your convenience
        // since you don't like the excess code when accessing the variable
        public int getSomeVar() {
            return myVariableWrapper.some_var;
            // or myVariableWrapper.getSomeVar();
        }
        public void setSomeVar(int newvar) {
            myVariableWrapper.some_var = newvar;
            // or myVariableWrapper.setSomeVar(newvar);
        }
    }
    

    Now you can do this:

    MyClass2 myClass2 = new MyClass2();
    System.out.println(""+myClass2.getSomeVar());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Straightforward question: I have a few variables that are doubles. I would like to
I have a string that looks like this: (12.0+10)*31 I need to be able
I am working on a Java program, and I really need to be able
I really need advice on how to do the following. I have tried several
I really need some help in Regular Expressions, i'm working on a function like
I am incredibly new to Python and I really need to be able to
I'd need to be able to combine 2 lambda expressions into 1: This would
Hey there. I would really appreciate your help in creating a query that would
I have a program in Perl I'm working on where I would need multiple
I really need to be able to perform a 'DISTINCT' on a WCF Data

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.