How can I find out what variables exist in an object?

I was given this code:

//Class X is created, and then class Y is derived from class X:
class X
  {
  protected int m;
  }


class Y extends X
{
  private int n;
  public Y (int m, int n)
    {
     this.m = m;
     this.n = n;
    }
  public String toString ()
    {
     return m + ", " + n;
    }
}

//Class Y is used in the following way:
class UseY
{
  public static void main (String[] args)
  {
    Y y = new Y (3, 4);
    System.out.println (y);
  }
}

      

So, as you can see, the code includes inheritance.

I was asked:

What is the result of the UseY program? What variables are there in the y object?

My answer:

Output: 3.4. The variables in the y object are m and n.

But I'm not sure about my second answer. What do they mean by variables exactly? Is my answer correct?

+3


source to share


3 answers


I would say:



Object y has a sub-element called n , and is derived from a member of its superclass called m .

+1


source


Your answer to the first question is correct.

What do they mean by variables exactly?

This page explains the exact meaning of the variables.



The Java programming language defines the following types of variables:

  • Instance variables (non-static fields) . Technically speaking, objects store their separate states in "non-static fields," that is, fields declared without a static keyword. Non-static fields are also known as instance variables because their values ​​are unique for each instance of the class (for each object, in other words); The currentSpeed ​​of one bike is independent of the currentSpeed ​​of the other.

  • Class Variables (Static Fields) A class variable is any field declared with a static modifier; this tells the compiler that there is only one copy of this variable, no matter how many times the class is instantiated. The field defining the number of gears for a particular bike type can be marked as static, since conceptually the same number of gears will apply to all instances. Static int numGears = 6; will create such a static field. In addition, you can add the final keyword to indicate that the number of transfers will never change.

  • Local variables . Similar to how an object stores its state in fields, a method often stores its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword that defines a variable as local; this definition comes entirely from where the variable is declared, which is between the opening and closing method shapes. Thus, local variables are visible only to the methods in which they are declared; they are not available from the rest of the class.

  • Parameters . You've already seen examples of parameters, both in the Bicycle class and in the main "Hello World!" expression. Recall that the signature for the main method is public static void main (String [] args). Here the args variable is a parameter to this method. It is important to remember that parameters are always classified as "variables" and not "fields". This also applies to other constructors that take parameters (for example, constructors and exception handlers), which you will learn about later in the tutorial.

In this case, the variables present are instance fields n

and m

(type 1 above). The type object Y

has n

already defined in its class. Because it extends X

, it inherits the class member field m

. A subclass inherits all public and protected members of its parent. Therefore, the variables present in the type object Y

are the members m

and n

.

0


source


An object y of type Y contains two variables; int "m" and int "n" Output: 3, 4

0


source







All Articles