3

I have button and I'd like to do something like this:

Button b=new Button(){public int a;};
b.a=5;

How to do this in java?

5
  • 5
    You are extending the class there. Also, you can't. Commented Mar 6, 2015 at 2:18
  • Come on, there must be a way. Commented Mar 6, 2015 at 2:23
  • There is a way, but not there. Commented Mar 6, 2015 at 2:23
  • 2
    @oneat The fields in an object depend on its class. If Button does not have a field a, and you want an object that's a Button and has a field a, then you must create a new class that extends Button and has a field a. Commented Mar 6, 2015 at 2:27
  • Java is built on this idea of classes, extends, and interfaces. An interface acts like a mask with holes in it exposing only a few member variables and methods... you can always extend a class, but java is kinda strict in that you can't add variables in on the fly. Like the answers given, extends is what you want, and it basically makes an editable copy of the class for you, where you get all the benefits of the parent class and also can add your own jazz. Have fun learning Java, might I recommend taking a look at Clojure when you feel you need more expressivity (= Commented Mar 6, 2015 at 2:29

4 Answers 4

1

There is no way to add variables to a class without extending it. However, the extending class does not need to be named: you could use an anonymous class instead, in a way similar to the snippet from your post.

The trick, however, is to access the variable after you have added it: you could do it by mutating objects referenced from the class, like this:

// Prepare a mutable object for use in your class
final AtomicInteger a = new AtomicInteger(123);
Button b = new Button(){
    public void someMethod() {
        ...
        int n = a.intValue();
        ...
    }
};

When you set things up this way, changes to a's state in your method would become accessible to someMethod() of the anonymous subclass of the Button class.

Sign up to request clarification or add additional context in comments.

Comments

1

It seems like you want a custom Button object.

Simply create a new Class with extends Button and declare in that class the a variable and any other methods that you want to add.

Something like

public class SomeClass extends Button
{
   public int a;
}

public class SomeOtherClass
{
    public static void main(String args[])
    {
        SomeClass someClass = new SomeClass();
        someClass.a = 5;
    }
}

If really you don't want to extend the class, then it is impossible. Sorry.

Also, your example is actually creating an anonymous class which extends Button.

Comments

0

Without extending the class? Sorry. You make an extending class. Get used to it.

public class OneAtButton extends Button {
    public int aWellNamedClassMemberVariable;

    OneAtButton () {  // constructor
       aWellNamedClassMemberVariable = 5;
    }

} // class OneAtButton

Comments

0

You cannot create a new variable for a class within the same line that you creating a new object of that class. If you would like to add a new variable to the button class you have to either, modify the class to include your new variable or create a class extension like Jean-François Savard is saying.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.