ZpCikTi ZpCikTi - 2 months ago 6
Java Question

I've got trouble with inner and static inner class at java

Firstly, Thanks everybody that read that topic.

How can if statement become true in test class? I couldnt find any solution.I couldnt write any code in these method.I tried to send from Room class numberOfTiger to class Question's method but I didnt achieve that.

That's question about ,How can I change int variable(numberofTiger) to Cat.Tiger variable.After that if statement become true to invoke (getNumberOfTiger) method.

public class Test {

public static void main(String[] args) {

Animal an = new Animal();
Animal.Cat an1 = an.new Cat();
Animal.Cat.Tiger an2 = an1.new Tiger(3, 900, 2);

if (Animal.Question.getnumberOfTiger(an2) == 3) {
System.out.println("True");
}
}
}

public class Animal {

Cat[] c;

// inner class
class Cat {

Tiger[] t;

// inner class
class Tiger {
private int numberOfTiger;
private int averageOfTigerWeigth;
private int youngTiger;


public Tiger(int numberOfTiger, int averageOfTigerWeigth, int youngTiger) {
super();
this.numberOfTiger = numberOfTiger;
this.averageOfTigerWeigth = averageOfTigerWeigth;
this.youngTiger = youngTiger;
}

static class Question {

static int getnumberOfTiger(Cat.Tiger a) {
return 0;
}
}
}

Answer

In addition to either making Cat a static class, or using its instance,

you also need a getter for a.numberOfTiger since it is private, in Tiger class:

public getNumberOfTiger() {
return numberOfTiger;
}

Then:

return a.getNumberOfTiger();