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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:27:17+00:00 2026-05-30T00:27:17+00:00

I’m building a much much larger program, and I’ve tried several methods (all of

  • 0

I’m building a much much larger program, and I’ve tried several methods (all of which work) but I’m liking this method currently, although I don’t know if certain aspects of it represent bad programming practice. The code used in this example is just to get the idea across without pasting the whole code.

This code (pasted below) creates a new ClassMain object with a label and a static method to edit the label. ClassEditor is instantiated from ClassMain, which returns a button.

Now here is where I want to know if it’s bad practice, I have an action on the button which, when clicked, calls the static method in ClassMain and sets the label to a random number. The reason I’m wondering whether it’s bad practice is because I don’t actually call the method from a direct instantiation of the ClassMain object, I just do: ClassMain.setLabel("");. And I’m not entirely sure what this is calling. I have one instantiation of ClassMain, but if I had multiple, would it still work? So how can I edit aspects of a created object through this way of doing it rather than using a reference variable? If I had multiple classes would it create issues?

Sorry if these questions are rambled, it’s hard to ask exactly. I’ve provided the code below so you can see what I’m on about.

PS: Relating to the issue of if it would be an issue of more than one object of ClassMain, I created another an both buttons in both windows only updated one label. Why is this? And does this mean it’s not bad practice if used for one instantiation but bad if used for more? I hope someone can help me out with these issues!

ClassMain:

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;


public class ClassMain extends JFrame {

    private static JLabel l;

    public static void main(String[] args) {
        new ClassMain();
    }

    public ClassMain() {
        super("This is my app");
        setSize(450,80);
        setLayout(new GridLayout(0,2));
        l = new JLabel("Hi");

        ClassEditor ce = new ClassEditor();

        add(l);
        add(ce.getButton());

        setVisible(true);
    }

    public static void setLabel(String stringA) {
        l.setText(stringA);
    }

}

ClassEditor:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;


public class ClassEditor implements ActionListener {

    public ClassEditor() {
        ClassMain.setLabel("Click the button for a random number!");
    } 

    public JButton getButton() {
        JButton b = new JButton("Click me!");
        b.addActionListener(this);
        return b;
    }

    public void actionPerformed(ActionEvent arg0) {
        int i = (int) (Math.random()*10);
        ClassMain.setLabel("Random Number: "+i);
    }

}

Big thanks to anyone who can help me out, very much appreciated. Just trying to learn and understand good practices and why they work.

  • 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-30T00:27:19+00:00Added an answer on May 30, 2026 at 12:27 am

    I probably wouldn’t use static metods and variables and simply rewrite it like this (I also changed names – a good practice is to have everything named in a way that everyone knows what does it mean):

    ClassMain:

    import java.awt.GridLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    
    public class ClassMain extends JFrame {
    
        private JLabel label;
    
        public static void main(String[] args) {
            new ClassMain();
        }
    
        public ClassMain() {
            super("This is my app");
            setSize(450,80);
            setLayout(new GridLayout(0,2));
            label = new JLabel("Hi");
    
            ClassEditor classEditor = new ClassEditor(this);
    
            add(label);
            add(classEditor.getButton());
    
            setVisible(true);
        }
    
        public void setLabel(String text) {
            label.setText(text);
        }
    
    }
    

    ClassEditor:

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    
    
    public class ClassEditor implements ActionListener {
    
        private ClassMain classMain;
    
        public ClassEditor(ClassMain classMain) {
            this.classMain = classMain;
            classMain.setLabel("Click the button for a random number!");
        } 
    
        public JButton getButton() {
            JButton button = new JButton("Click me!");
            button.addActionListener(this);
            return button;
        }
    
        public void actionPerformed(ActionEvent event) {
            int i = (int) (Math.random()*10);
            classMain.setLabel("Random Number: "+i);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.