Win a copy of Rust Web Development this week in the Other Languages forum!

Harish Shivaraj

Ranch Hand
+ Follow
since Jan 08, 2011
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
24
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Harish Shivaraj

Thanks Henry.
The current design i have is that, I call the .log method manually. I wondered if there was some way (design) that the method can be called automatically when the object is either returned or about die. If Java controller has some way to invoke it.

I suppose your post sort of concludes the "finally" is the way to go forward.
 
5 years ago
Just to clarify that, its good enough for me if method get called just before it dies is still valid. However from my code snippet its probably not clear. But the over all intention is ofcourse that I would like to log the object close to the timeline of object death.
5 years ago
Actually your right. The object at the point of return is still alive and not dead. Perhaps the question is really that, is there a way I could invoke the class method when the object is returned?

5 years ago
Hi all.

Is there a way to call class method when is an objects dies. Take for instance



what I want is; log the response object before it dies. Now I want the logging process to be an implicit part of the code rather than explicit.
I could call the log method when the object is created. Which was easier. However, when the object is created not all attributes are set and therefore logging then makes no sense.
From the above code, by the time I return the object I have the state of the object complete and therefore it make sense to log the object then. The one obvious way to do this to just call the log method just before returning.



But is there a better design than that. Such as destructor (I know java doesn't have a destructor method). Is there something similar to compensate?

Thanks
5 years ago
>  but if i do then the variables inside of the loop block are out of scope and cat call them.
Any suggestions?

That's not entirely true. Your only printing the variable count which is declared outside the loop and should have no scoping issues. If you do; can you show us the  error message you get?
5 years ago
Actually I just read you latter part of your message, which implies you've already tried my suggestion.
5 years ago
Remove your print statement from inside the loop to outside.


5 years ago
Thanks very much Junilu. I appreciate your help and I will look into Dependency Injection
5 years ago
Here is the cut down version of my original code.



There are other classes which subclass which inherits from Controller. But with this example ive shown UserController. As you can see, what i was trying to do is to put the common code to the superclass Controller and generalise it, such that it can be used by other classes.

Also why dont I have an option to edit my posts after being posted?
5 years ago
Ok I dont think my previous post is a good design. I really need to rethink through this.

I only read Junilu last post.
5 years ago
I terribly sorry fr my poor explanation of the problem.

I understand the point that that constructor should never call overridable methods. If I take the example from your post Junilu and modify slightly to



Is that a good design? This certainly donst overrule the fact that superclass constructors never call overridable methods. If Im understanding that right?

Many thanks for your help and the patience. I do appreciate it.
5 years ago
Just to add - what I have right now is what's in my 2'nd post. where in have initialise method in the superclass which calls to init1 init2. And call the initialise in sub class constructor.

Is that a good design?
5 years ago
What I really want is this. I have need to initialise couple of variables which is protected visibility in the super class. I have two abstract methods within the superclass which enforces to implement those method in the subclass. This is required to be implemented by subclasses before calling initialise method. Because the initialization process requires values from those variables.

Having initialized the variables, the method initialization will need to be called.

I can think of either the way which was already pointed out in this thread or I can make the initialize method to be abstract in the super class and document to make sure that when the subclass constructor to call the initialize method.

Or any other suggestion?

5 years ago
Thanks very much all. I think the Dave sample code clearly explain the enforcement rule. That is the the method must be overridden and those methods are invoked by the superclass, even through the subclass fails to call it. This is precisely what I was looking for.

Thanks Campbell, Junilu and Dave.
5 years ago
I really must update the code here. The example code in the previous post which donst implement the abstract method in the inherited class, which is wrong. The code should look something like this


5 years ago