• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Calling Constructor Within Method

 
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I needed to write a method that will come up with one day before than the day that object sent in. I wrote it but the problem is that I need to change already constructed Object's values.

There is a constructor that takes month,date,year inside current class DecDate as well as parent class Date, but super doesn't work since I can't use it after all calculations and I was not able to use constructor within same class.

Any improvement beyond this issue on the code is appreciated as well!

My Method
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Recaip,
First thing is please put your complete code the parent as well as the sub class,it would let us to help you better.
Second thing you cannot call a constructor inside the method(you can make use of it to create an object with *new* keyword).constructor can be called in another constructor at very first statement.
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand what you are saying, then change "super" to "this" or just remove the "super". Doing so will allow you to use the methods in your current instance.
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you are required to make some changes for the 3 variables then you can do it in the sub class constructor like below

If you want it to set in method then you can do it by providing a setter(but it will make your class mutable) like

IMPROVEMENTS:
you can refactor your method(as it's doing the 3 task,try it yourself).
anyway you can Indent Your Code better-look at line 1 and 6 find the difference.
you are dealing with object so try to stick with Object Oriented(OO) design.
you are extending a class just to add some utility.don't you think you can add this same utility in the same class.favor Composition over Inheritance.Ask yourself 2 things every time when
you are making use of inheritance:Sub Class *Is-A* Super Class(it should hold in every aspect just like Dog *Is-A* Animal holds in each aspect,
even if a single reason makes you to think that this condition might not hold then avoid inheritance) and Sub Class *Has-A* Super Class(if it meets
your need than avoid inheriting the super class).
try to Refactor the methodsmake use of Single level of abstraction principle(SLAP) and Composed Method Pattern.

Hope it helps!

Kid Regards,
Praveen.
 
Recaip Sanli
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you @praveen kumaar, I am not allowed to make any change anywhere else besides inside of decrement method.

Date.java


DecDate.java


TestDecDate.java
 
Recaip Sanli
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Les Morgan, no this.DecDate(month,date,year); doesn't work either.

 
Marshal
Posts: 80472
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what do you think that error message means?
 
Recaip Sanli
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:And what do you think that error message means?



I found that error caused by Date, I was suppose to type in day. I fixed that now there's another problem



DecDate constructor accepts int,int,int and I am sending int,int,int why that's an issue?
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Recaip Sanli wrote:
DecDate constructor accepts int,int,int and I am sending int,int,int why that's an issue?



You can't call constructors like methods. Constructors are *not* methods.

You can, however, instantiate another object, which, in turn, calls the constructor... but you can't call constructors like methods.

Henry
 
Recaip Sanli
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Recaip Sanli wrote:
DecDate constructor accepts int,int,int and I am sending int,int,int why that's an issue?



You can't call constructors like methods. Constructors are *not* methods.

You can, however, instantiate another object, which, in turn, calls the constructor... but you can't call constructors like methods.

Henry



Do you mean casting it to another object?

I am thinking I need to change object that is sent in and send back a new object with correctly calculated decremented date.

Does anyone have any idea to get this done, without coding anything outside decrement method? or is what I am thinking not possible?
 
Saloon Keeper
Posts: 11027
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Date class contains a lot of deprecated methods and constructors which you are using. This is very bad practice. For example:

For your decrement() method, the only Date method that allows you to modify a Date object in place is setTime(long), so you'll have to figure out a way to convert your month/day/year to a long and then call setTime() (hint: Calendar). Alternatively you could look into the java.time.LocalDate class.
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Recaip Sanli wrote:
Does anyone have any idea to get this done, without coding anything outside decrement method? or is what I am thinking not possible?



Do you want the decrement() method to return a new date? Or do you want the decrement() method to change the date of itself (ie. the this instance)?

For the first, you can just use the new operator -- ie. instantiate and return a new Date object from the method. And for the second, why can't you just change the instance variables directly?  After all, the variables are protected, and hence, can be changed by the subclass.

Henry
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:The Date class contains a lot of deprecated methods and constructors which you are using. This is very bad practice. For example:



FYI... the OP has implemented a Date class. This topic is likely not about the Date class provided by the core Java environment.

Henry
 
Saloon Keeper
Posts: 28578
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Recaip Sanli wrote:Thank you @praveen kumaar, I am not allowed to make any change anywhere else besides inside of decrement method.



If this is a requirement from an employer, I encourage you to find another employer as soon as possible. Anyone who micro-manages to that degree is not going to be good for your state of mind, your health, or even your future career path.

If this is an academic assignment, it's probably expecting some sort of "correct" answer - and the cynic in me notes that often such exercises require not merely a certain level of competence on the part of the student, but also a certain level of ignorance. I often get annoyed with tests that allow for fewer correct answers than I can actually supply.

In the "real world", I deal with this sort of problem all the time, but since I am not constrained to someone's idea of a "correct' solution, this is what I do:

I define a method named "init()". It may be coderanch, private or protected, depending on the scope I need to reset the object from. Instead of initializing the basic object from the constructor, the constructor invokes init(). When I need to re-initialize the object, I invoke init() again.

Of course, given your Procrustean constraints, that would be forbidden, and failing that, the closest reasonable approximation I could do would be to copy-and-paste the constructor logic into the decrement method. Which will still cause blame to be assigned to you, but it's about the best I can suggest.

Constructors are technically methods, but their true names are not actually what you code and they are invisible to application code. Only the compiler internals can see them. Which is why a constructor can invoke an init method, but an init method cannot invoke a constructor - other than as a side-effect of instantiating a completely different object, anyway.
 
Recaip Sanli
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Henry Wong, I think I need to modify object sent in and override it with decremented date and send back. I am guessing this from test driver he coded, I am not fully certain if that's what I am suppose to do..

@Tim Holloway, Thank you for your deep insights. I feel like there is no point to reinvent the wheel or try to push the car without wheel. We must catch up to latest knowledge in the field and use what is invented and provided like Date class provided or Calendar, but it's a class assignment so it's what it is..

It's solved out everyone, thank you @Henry Wong. I did change parent's instance variables. with below code



Also, had to make corrections to the if statements, below is correct version


 
reply
    Bookmark Topic Watch Topic
  • New Topic