0

I am new to java and still got a lot to learn. I have embedded C programming experience but totally new to object oriented programming.

I got the following that i need to understand. When you create a class in Java it is the blueprint of an object you are going to create. And say i have a GUI with a button that creates a new bicycle after i have clicked the create new bicycle button you enter all the instance variables that it asks for in your GUI etc.

Then when you finish with say clicking on a complete button a object is created. With all the data you supplied in your GUI to fit the blueprint.

So in your actionlistener when the complete button is pressed a piece of code like Bicycle bike = new Bicycle(); should run. But bike is static and you never know how many entries of bicycle's there is going to be. So how can the reference variable be made dynamic or act like its dynamic?

Sorry let me try and clear it up with the following.

public void actionPerformed(ActionEvent e) {
            Bicycle bike = new Bicycle();
    }

this is a action which is performed when Jbutton is pressed. This will create an object named "bike". But what happens when button is pressed again?Will it just override the previous object named bike?

And you dont know how many objects of bicycle type the user will create by pressing the button. So i was wondering if something like a array can be used in the following way

String bike[] = {"bike1","bike2,"bike3"};
for(int i=0;i<3;i++){
    Bicycle bike[i] = new Bicycle();
}

or use a 'array list' because ultimately you wont know how many object will be created?

4
  • bike is not static or anything like it, and the JVM does far more things dynamically than you might be used to. Commented Jan 20, 2015 at 14:55
  • Could you clarify what you mean by the difference between a static and dynamic reference? Commented Jan 20, 2015 at 14:55
  • What type of dynamic behavior are you looking for from the reference variable? Also, the bike object is not static, it would be an instance variable - unless you mean it is "static" in the sense that it cannot take on another type of object. If you can explain more to this it might help. Last, if instead of Bicycle bike = new Bicycle() you used `Object bike = new Bicycle()', that would be more generic. Commented Jan 20, 2015 at 14:55
  • I'm finding it hard to understand your question. What do you want to be dynamic? If you want potentially multiple items, you should use a collection of some kind, e.g. ArrayList<E>. If you don't know what type you're going to create instances of, you can use Object - but you'll need to use reflection to create the instances themselves... Commented Jan 20, 2015 at 14:56

2 Answers 2

1

So how can the reference variable be made dynamic or act like its dynamic?

This is how reference behave by default. There is nothing static about them. Even if you use a static field it is not static in the sense C would understand it. It doesn't exist until the class is loaded and it can exist multiple times if the class is loaded in different class loaders. It can even go away if the class loader is unloaded.

If your field is not static it is even more dynamic esp if it is a local variable.

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

Comments

0

If you want a variable that can hold references to multiple Bicycle instances, you can use a Collection such as List :

List<Bicycle> bikes = new ArrayList<Bicycle>();

Then, each time a new Bike instance is created, you add it to the List :

Bicycle bike = new Bicycle(); 
bikes.add (bike);

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.