1

I wanted to find the youngest age inputted among x amount of pets. I have tried putting 'public static int youngestAge'; however, that just gets me the latest assigned value to the variable 'youngestAge'. I think this is because of the keyword 'static'; so, I tried removing it, but that just results in netbeans telling me 'Non-static variable cannot be referenced from a static context' . I am expecting the code to give the lowest inputted age.

package pet;
import java.util.Scanner;
public class Pet 
{
    public static String petName; 
    public static int petAge, petWeight;
    public int youngestAge; 


    public static String setPetName()
    {
       Scanner input = new Scanner(System.in);
       petName= input.next();
       return petName;
    }

    public int setPetAge()
    {
        Scanner input = new Scanner(System.in);
        petAge= input.nextInt();
        return petAge;
    }

    public int setPetWeight()
    {
        Scanner input = new Scanner(System.in);
        petWeight= input.nextInt();
        return petWeight;
    }


    public void getYoungestPet()
    {
        if (youngestAge<petAge)
            youngestAge=petAge;
        System.out.println("The youngest age is " + youngestAge);
    }
}

    package pet;
    import java.util.Scanner;
    public class PetMain extends Pet
    {

        public static void main(String[] args)
        {

        System.out.println("How many pets do you want to enter? " ); 
        Scanner data= new Scanner(System.in);

        int petNumber=data.nextInt();

        for (int i = 1;i<=petNumber; i++)
        {
        Pet PetObject = new Pet(); 

        System.out.println("Please enter name for Pet " + i );  
        PetObject.setPetName();          
        System.out.println("Your pet's name is : " + petName);
        System.out.println(" ");
        System.out.println("Please enter " + petName + "'s Age" );
        PetObject.setPetAge();
        System.out.println("Your pet's age is : " + petAge);
        System.out.println(" ");
        System.out.println("Please enter " + petName + "'s Weight" );
        PetObject.setPetWeight();
        System.out.println("Your pet's weight is : " + petWeight);
        System.out.println(" ");

        if (youngestAge<PetObject.petAge)
                youngestAge=PetObject.petAge;
        }
        System.out.println("The youngest age here is : " + youngestAge );
}
  • Hereafter looking at your code, it seems like you just need to declare `int youngestAge=0;` inside main method of your `PetMain Class` – Dushyant Tankariya Jul 06 '19 at 04:57
  • 1
    mixing `static` and non-`static` - I suggest you go learn about its meaning (class fields and instance fields). It is an important aspect of object oriented programming – user85421 Jul 06 '19 at 05:07
  • I guess you have to brush up your JAVA little more. You can not mix static and non static without understanding their purpose. If you declare anything static, those are class level so they will have only one copy of it. On the other hand, non static fields/methods are object level, you need to create objects to access/call them. I am sure you will find bunch of article online explaining this. Just google it. – b1k Jul 06 '19 at 05:42

1 Answers1

0

The if condition used is wrong. It will check if current Pet objects age is greater than youngestAge then assign youngestAge as current pet object's age.

if (youngestAge<PetObject.petAge)
      youngestAge=PetObject.petAge;
}

This will produce oldest age instead of youngest.

To get youngest age you need to assign youngestAge only when current Pet object's age is less than youngestAge to get minimum age.

The error which you are getting is because you are trying to access instance variable of Pet class inside static method main.

Also youngestAge should be part of your Pet class. The a pet should not know what is the youngest age so far for other pets. youngestAge should rather be member of other class like PetMain class.

You can either make it a static variable and then access it from main class or make it a local variable inside main method.

ashish2199
  • 393
  • 3
  • 13