Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Overloading Constructors. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overloading Constructors." Watch "Overloading Constructors." New topic
Author

Overloading Constructors.

Grant Gibson
Greenhorn

Joined: Nov 24, 2011
Posts: 10
I'm trying to create multiple constructors, one that is a default value, of 100, and the other, if the user chooses to do so, has a value of whatever they decide. I'm confused on how to make the constructors, and allow them to be edited by the methods below. However, the constructors are my main priority. The methods I'll about when I correct my constructor situation.

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
Why are you initialising static variables in a constructor ? You do realise the value will be overwritten for every instance of your class whenever you create a new instance.


Joanne
Grant Gibson
Greenhorn

Joined: Nov 24, 2011
Posts: 10
Joanne Neal wrote:Why are you initialising static variables in a constructor ? You do realise the value will be overwritten for every instance of your class whenever you create a new instance.


I didn't realize that. Our instructor didn't go over constructors very well, so I'm trying to learn a lot of it on my own. Even if I take static out, I'm still registering errors in eclipse.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
Post the new code and the errors (cut and paste them - don't try to paraphrase) and we'll see what we can do to help.
Grant Gibson
Greenhorn

Joined: Nov 24, 2011
Posts: 10
Joanne Neal wrote:Post the new code and the errors (cut and paste them - don't try to paraphrase) and we'll see what we can do to help.




Overall, the code just doesn't seem like it's going to do what I want it to do, as of right now. Default = 100, anything else equals the users input.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
Can you give an example of what it should be doing and what it is actually doing. That code looks fine to me.

Edit - having said that, your buypoints method looks wrong. Shouldn't it be adding amt to startbalance - you don't have a balance variable anymore ?
Grant Gibson
Greenhorn

Joined: Nov 24, 2011
Posts: 10
Joanne Neal wrote:Can you give an example of what it should be doing and what it is actually doing. That code looks fine to me.

Edit - having said that, your buypoints method looks wrong. Shouldn't it be adding amt to startbalance ?


Now that I have the class created, I want to use it in the main class. So lets say I created a new MealCard.



Shouldn't I be able to out.print a line that has the meal card amount? Which, I think it should print 100, seeing as that is the default value.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4168
    
  21

First, as previously asked, please copy and past the real error message, don't paraphrase, because you might be pointing us to the wrong position.

Second, You have an error on line 25 of the code you posted:

The variable balance does not exist.

Third, you might try saving the .java file, deleting any .class file, and recompiling just in case you aren't compiling current code


Steve
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4168
    
  21

Grant Gibson wrote:
Shouldn't I be able to out.print a line that has the meal card amount? Which, I think it should print 100, seeing as that is the default value.


Only if you have a means to access the amount. For example, a getBalance() method which returns the value you want to print, or a toString() method which converts the contents (balance and other information if present) into a displayable String.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
You would need to add a method to your Mealcard class to return the current balance. Something like

You could then call this from your main class with

Grant Gibson
Greenhorn

Joined: Nov 24, 2011
Posts: 10
Steve Luke wrote:First, as previously asked, please copy and past the real error message, don't paraphrase, because you might be pointing us to the wrong position.

Second, You have an error on line 25 of the code you posted:

The variable balance does not exist.

Third, you might try saving the .java file, deleting any .class file, and recompiling just in case you aren't compiling current code


Alright. I changed balance to startBalance.

I'm using eclipse to compile and run, so I should be running the current code (hopefully, ha).

And the "error" I'm getting is not really an "error", it's just out.printing wrong. But here it is,

"MealCard@199a0c7c"

While I'm using this as the main class.



I just want to make sure the MealCard class is actually defining the value at 100, or whatever value is inserted. Which it is not doing.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

You need to override toString() in your MealCard class. What you're seeing is the default implementation provided by Object - the class name followed by @ and the hexadecimal hash code.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Grant Gibson
Greenhorn

Joined: Nov 24, 2011
Posts: 10
Wow. I feel dumb =.=. Now I should be able to start working on my main class with no problems. -fingers crossed-

Thank you for your patience by the way, I haven't really had the chance to sit and ask questions with someone fluent in Java.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Welcome to the Ranch

You need an instance field with the balance in; this will be different for each instance. At least you have the value in integer format; that means it will be limited in value (if that means pence, then you need to keep the balance less than slightly over £20000000 [or $20000000, etc.]).
If you want a static field as a constant to maintain a default start balance, that is a good idea. It should really be final so it never changes.
I usually warn against no-arguments constructors, but in the case where you want a default initial value (100), that is all right. You want (startBalance) to pass to the other constructor. In the other constructor, you want to initialise the value of balance, which is not static.
You will, of course, use balance, not startBalance, throughout the rest of the class.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Overloading Constructors.
 
Similar Threads
Help code problem please
doubt on Threads
Having trouble understanding an error code i keep getting. Please help!
Value Objects or bean is better and why ?
Loop problems