3

Just for reference, I've taken like one high school class on Java, so this question should be super easy to respond to. I'm just having trouble figuring out a way to call a class method, the constructor, as a new object without putting in new values for the parameters. The constructor looks like this:

public Skills(int courage, int intelligence, int stamina, int crafting, 
int blacksmithery, int herbalism, int slingSkill, 
int bowSkill, int swordSkill, int armor, int stealth, int 
lifeForceSkill){
    this.courage = courage;
    this.intelligence = intelligence;
    this.stamina = stamina;
    this.crafting = crafting;
    this.blacksmithery = blacksmithery;
    this.herbalism = herbalism;
    this.slingSkill = slingSkill;
    this.bowSkill = bowSkill;
    this.swordSkill = swordSkill;
    this.armor = armor;
    this.stealth = stealth;
    this.lifeForceSkill = lifeForceSkill;

} And when I establish it it my main method I do this:

Skills skills = new Skills(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);

To set all the levels to 1 and then I have the object skills to work with later. (Am I getting the terms constructors and objects mixed up? If it's wrong just switch them all in your mind I think I'm being consistent with it at least).

The problem is I have a toString() method in the Skills class that I want to call in a separate method in the main class.

else if (optionChoice.equalsIgnoreCase("View Skills")){
    Skills skills = new Skills();
    System.out.println(skills.toString());
    break;
}

Of course the object I create here throws an error because I cannot just write Skills(); without putting in all twelve values for the parameter. So how do I call the method from the Skills class when I can't create an object for it? It's imperative to functionality that the values do not change, and since they are variable and change with the program, I can't put any value in for them.

I know it's probably a super basic question but that's the level I'm at so any super basic answers would be helpful. Shouldn't take too much of your time. Thanks in advance!

Enoch
  • 51
  • 2

4 Answers4

3

In your main method your "skills" object should be passed around if you want to keep its values. What you're currently doing is making a new object (constructing one) every time you call a function.

A flow as follows is what you want.

Skills skills = new Skills(...);
.... 
skills.toString();

Like you said if you want the Skills object in another method in your main class just pass the skills object to that method. Like so:

public static void main (String args[]) {
    Skills skills = new Skills(....);
    fooMethod(skills);
}

fooMethod(Skills skills) {
    // Here you can :
    skills.toString();
}
Zachary
  • 1,693
  • 1
  • 9
  • 13
Arno C
  • 470
  • 4
  • 18
2

Declare skills as a member variable, more commonly called a field (outside of your main), then initialise it like your doing now. Then you can access it with skills. toString() from any method in your class.

Cardinal System
  • 2,749
  • 3
  • 21
  • 42
O.O.Balance
  • 2,930
  • 5
  • 23
  • 35
2

Overload the constructor with something that has a default functionality.

public Skills() {
     this (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
}

However, if the Object itself is not representative of anything, and only instantiated for the purpose of accessing the toString method, you should create a Static method. Static methods can be accessed by invoking on the Class itself, not on the object. Though this highly depends on your intended purpose.

public static toStaticString() {
    return "Static String";
}

If you are wanting to retain a Skills object throughout, you will need to pass around the object and invoke methods on it. Remember that instantiating Skills creates a new 'instance' of it, which will remember the various values for each of the skills belonging to it. A little more on this.

Note: The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class. - Java Docs

Zachary
  • 1,693
  • 1
  • 9
  • 13
0

Here is another answer as I haven't seen anyone post about it.

Generally there are many patterns for programming languages that have been created to fix many different problems. One of them is Builder pattern which is used as a fix for when there are many parameters to be passed to constructors. Now, I don't want you to get confused or anything as you specified you have attended only 1 Java class so far, but in case you want to know more you can just Google "java builder pattern" or after some time when you get more comfortable with Java you can read really good book called Effective Java 2nd Edition by Joshua Bloch which has a chapter on Builder pattern and many other useful things that you will eventually need if you opt for programming career.

whatamidoingwithmylife
  • 1,119
  • 1
  • 16
  • 35