Is it possible for a constructor to call a class which is related to it? For example (in the code below) I am trying to create a new clock with ClockTimer and call the AnalogClock construct which has a ClockTimer parameter type. It doesn't seem to work because it gives me an error, but creating a new AnalogClock with the ClockTimer parameter in the ClockFrame class appears to work. What do I need to parse in AnalogClock (in the ClockApp) in order for it to run?
From the outside, it looks like the "analogClock" object needs the "timer" object to exist first, and the "timer" object needs the "analogClock" object to exist first.
In reality, thought, "timer" doesn't do anything with the "analogClock" ("clockFrame" inside "ClockTimer"), so it actually doesn't need it.
I'd suggest the following:
Remove the "clockFrame" parameter and field inside "ClockTimer"
Create the "timer" object first
Create the "analogClock" object second, and pass "timer" to it
Related
This question already has answers here:
Reflection: get invocation object in static method
(4 answers)
Closed 7 years ago.
We all know that in Java you can call a static method as an instance method like this:
Foo foo = new Foo();
FooBar fooBar = foo.bar(); // bar is a static method on class Foo
What I want to know is:
Is there any way to determine inside bar whether bar was called statically (Foo.bar()) or called via a class instance as above?
If so, is there any way for bar to get a reference to the object which called it (in this case foo)?
Reason:
I am developing a kind of semantic syntax. I want my consumers to be able to put things like:
With.attribute("blah").and().attribute("blahblah"); // both "attribute" and "and" methods return an object of type "With"
Here you can see that attribute is being called both as a static and an instance method. However, you can't define a static and an instance method with the same name in Java, for the same reason as above - the static method could be called as an instance method and so to create an instance method with the same name would create ambiguity. Therefore I want to create a single method attribute which can be called both statically and non-statically, and inside the method body I want to try to determine if it was invoked statically or non-statically. The above questions will help me to assess the feasibility of doing this.
No, there is no way for the method to know whether it was called on a class or on an instance (at the JVM level there is no difference), and there is no way to get the instance that the method was called on.
The term for this kind of "semantic syntax" is domain-specific language (DSL).
Possible solution: name the static method withAttribute, then you could make it look like this:
withAttribute("blah").and().attribute("blahblah");
Java isn't made to do this, because it is not a very good thing to do.
If you want to be able to do something like this, why not do something like
new foo().withAttribute("blah").withAttribute("blahblah");
It really isn't a very good idea to do this at all.
Plus, why are you making your consumers use java code to give instructions? why not do something like
make a foo //Check for and chop off "make a" to get the object to create
add attribute blah //Check for and chop off "add attribute" to get the attribute to add
add attribute blahblah //Or, if you can add other things too, check for and chop off "add", then check for and chop off "attribute"
It seems like that would be much simpler.
I stopped at some breakpoint in some object instance. I have multiple instances of this class, called in various place.
Is it possible to find that constructor call, which has created namely this instance?
Constructor call is not in stack in the case.
No, that's not possible; as #ajb's comment says, the constructor call is long gone by the time the object is used in other ways.
One option you have is to set a breakpoint in the constructor and note the object ID of each instance as it passes through the constructor (Object ID is visible in the Variables view, as the value of this.
Then when the other breakpoint is hit, you can look at the ID and know which object it refers to.
I've looked everywhere but I can't seem to find an answer to this.
If I have one class (say, a Resource class) and another class (say a Sprite class) and each time I create a sprite, I pass in a reference to my Resource class (because it's required for some function) - am I correct in assuming, that all this does is creates a reference to this instance of said class?
So - if my Sprite constructor is this:
public Sprite(Resource res){
res.doSomething........
}
And I create 100 sprites, then this isn't going to cause problems because it's just passing in a reference or 'pointer'? (as opposed to creating a new instance each time).
Simple enough question I know, but I want to make sure I understand what's happening here and I couldn't find the answer to this anywhere.
Yes, if you create a new Sprite and pass it an existing Resource, the new Sprite will just have a reference to your original object.
So 100 Sprites would have 100 references to your 1 Resource.
Yes you have to create eachtime a new instance of the class to call doSomething() of that particular instance.
I am trying to pass an arraylist that will be populated in method A of Class X and tehn I want to use teh same arraylist in method B of the same class. But since I am using DWR to access my java classes from JavaScripts, every time I am calling a method it is instantiates the class again and I loose the data inside the Arraylist in the process.
I am wondering if there is a work around or a formal way of passing the array list.
Thanks!
So I have created an instance of a class by searching through jar files with no problems, I have it set to create an instance using c.newInstance() (Is instance the right word to use here?)
Later on in the program I may want to create another instance of that class if a certain event occurs. How can I go about creating this without having to search through all of the Jar files until I find the right one and then creating it again? Is there a way to create it somehow if I still have a reference to the first one?
Please assume that I do not know the name of the classes that will be loaded until runtime and there will be multiple classes that will be loaded.
Thanks
Save a reference to the Class of your object
Class c = dynamicObject.getClass();
and
and you can create a new instance like this (assuming there is a parameterless constructor)
Object anotherDynamicObject = c.newInstance(); // you can cast accordingly
else, say there is a consturctor that takes int, you can do
Constructor constructor = c.getConstructor(int.class);
Object anotherDynamicObject = constructor.newInstance(1);
Assuming "x" is the object you created ...
x.getClass().newInstance();