-2

I don't know if this is a wrong way to ask questions, as it's really a continuation of what I got a tip of in another question.

The thing is, I was wondering what static keywords did, and I think I understood it. My question now is, did I understand correctly? And how do I create instances of "Dogs"?

class Dog {
static String form /* of all dogs */ =  "Doggy-like";
static int quantity /* of dogs */ = 5;

String colour; /* of a specific dog */
String size; /* of a specific dog */ 

}

class Cat {
static String form /* of all cats */ = "Catty-like";    
static int quantity/* of cats */ = 3;

String colour; /* of a specific cat */
String size; /* of a specific cat */


}

public class Animals {
public static void main(String[] args){
    System.out.println("There are "+Cat.quantity+" cats.");
    System.out.println("There are "+Dog.quantity+" dogs.");
    /* EDIT: */
    Dog Mike = new Dog();
    Dog Pete = new Dog();
    Cat Sushi = new Cat();
    Cat Michael = new Cat();
    Cat Pete = new Cat();
    Dog.Mike.size="Big";
    Dog.Mike.colour="Red";
    Dog.Pete.size="Small";
    Cat.Sushi.size="Small";

    }
}

I'm also wondering if there is a conflict between those Pete cats and dogs, and if it's correct to define their sizes like that. Does it make a difference to create it inside public class Animals, or inside their respective classes (or another class, for that matter)?

Community
  • 1
  • 1
mazunki
  • 682
  • 5
  • 17
  • Looks like it. And you can create instances of a classes by calling the proper constructor, in this case it would be `Dog dog = new Dog();`. – Luiggi Mendoza Jan 10 '15 at 17:12
  • Not your down-voter, but please take care as you've asked several questions that have been down-voted, and if this happens too much, the site software can automatically block you from asking further questions. If you've not yet done so, please go through the [tour], the [help] and the [how to ask a good question](http://stackoverflow.com/help/how-to-ask) sections to see how this site works and to help you improve your current and future questions, thereby hopefully avoiding the ban. – Hovercraft Full Of Eels Jan 10 '15 at 17:14
  • And now for the next lesson, introduce a class Animal as an extension of Dog and Cat. – wvdz Jan 10 '15 at 17:16
  • @popovitsj Do you mean that I should put "Animals" on top? I figured that it read from top to bottom, so the variables I used had to be written beforehand. – mazunki Jan 10 '15 at 17:25
  • @HovercraftFullOfEels Okay, I'll read it through. Thank you ^^ – mazunki Jan 10 '15 at 17:25
  • @mazunki in java, the order doesn't matter for the compiler, if this was c++ you'd be right. Anyway, that's not what I meant. I was just sort of jokingly suggesting that now that you figured out static variables, maybe you should study inheritence next. – wvdz Jan 10 '15 at 17:29
  • @popovitsj Ah, I got it now. Excuse my confusion. Thanks for the tip, I'll try it. My idea: Get them all living on the same place. c: – mazunki Jan 10 '15 at 17:36
  • @HovercraftFullOfEels I read through it all, and see no reason for my negative votes. If you could clarify it, it would be appreciated ^^ – mazunki Jan 10 '15 at 17:38
  • @mazunki: again, they're not my votes, so I can't fully explain them. – Hovercraft Full Of Eels Jan 10 '15 at 17:38

2 Answers2

2

To create instance of Dog just do

Dog d = new Dog();

In this case the default constructor of class is getting invoked.

sol4me
  • 15,233
  • 5
  • 34
  • 34
2

Yes you have defined static field in the class and accessing it within static context with ClassName.fieldName. So you are right.

If you want to instantiate Dog, you could do so in your main like:

Dog dog = new Dog();

By default, we get constructor which doesn't accept any parameter/s.

SMA
  • 36,381
  • 8
  • 49
  • 73