1

I think I wrote the code correctly, but it does not print anything. Can anyone explain me why ?

public class Dogs {

    String name = "";
    String color = "";
    String breed = "";
    boolean hungry = true;

    void changeBarking (boolean True) {
        hungry = True;
    }

    void changeName (String newValue) {
        name = newValue;
    }

    void changeBreed (String newValue) {
        breed = newValue;
    }

    void changeColor (String newValue) {
        color = newValue;
    }

    void printStates() {
        System.out.println("name:" + name);
        System.out.println("color:" + color);
        System.out.println("breed:" + breed);
        System.out.println("hungry:" + hungry);
    }

}

class DogsDemo {
    public static void main(String[] args) {

        Dogs dog1 = new Dogs();
        Dogs dog2 = new Dogs();

        dog1.changeName("Dogas");
        dog1.changeColor("Juodas");
        dog1.changeBarking(true);

        dog2.changeName("Taksas");
        dog2.changeColor("Rudas");
        dog2.changeBarking(false);
    }
}
4
  • 7
    Why would it print anything? Commented Dec 28, 2013 at 16:04
  • 3
    If you don't call .printStates() it's hard. Commented Dec 28, 2013 at 16:05
  • I dont know why nobody is mentioning it, but wouldnt it be better to a) delete the printStates() method and move the print commands to the main method by calling dog1.variableName or b) overriding the toString method of the dog class...? Just a design hint anyway.. Commented Dec 28, 2013 at 16:23
  • Overriding toString() would be better IMO, but accessing the variable directly like dog1.variableName kinda goes against OO encapsulation principles. Those members should be private with setters/getters. Commented Dec 28, 2013 at 17:07

6 Answers 6

9

You have to actually call the method that does the printing.

Add

dog1.printStates();
dog2.printStates();

This seems a little awkward as well:

void changeBarking (boolean True) {
     hungry = True;
}

Variable names should start with a lowercase, and calling a boolean variable True is bound to cause confusion. Instead use something like this:

void changeBarking (boolean isBarking) {
     hungry = isBarking;
}
Sign up to request clarification or add additional context in comments.

Comments

3

you are not calling method printStates().

Comments

3

Try adding the lines

dog1.printStates();
dog2.printStates();

in your main() method after you set all your values. Currently you never actually make a call to your printing method.

Comments

1

System.out.println(""); allows you to print String. It's what you're doing in the printStates() method, but since you're not calling it at any time, it is not executed.

Comments

0

Because You are not calling the methd printStates()

Call the method as

dog1.printStates();
dog2.printStates();

Comments

0

The static before the main method, this declares the method as a class method, which means it needs no instance to be called. So as you are going to call a non static method, Java complains because you are trying to call a so called "instance method", which, of course needs an instance first.

Just call dog1.printStates(); and dog2.printStates();inside main method .

class DogsDemo {
    public static void main(String[] args) {
       //  code here
        dog1.printStates();
        dog2.printStates();
    }
}

See this simple program:http://www.daniweb.com/software-development/java/threads/366637/calling-methods-from-main-method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.