Doubt about class definition

1

Good morning. I'm new to JAVA. My question is about the definition of a method whose return value is of the same type as the class that is being defined. I really do not understand how this mechanism works.

I leave an extract of a class to show my doubt. Also I do not understand how you can define a Fraccion c=new Fraccion(); object within the class definition Fraccion .

Within the methods we define Fraccion c=new Fraccion(); that is of the same type of the class that is being defined and the add and subtract methods that return a data of type Fraction.

public class Fraccion {

       private int num;
       private int den;

       public Fraccion() {

        num=0;
        den=1;
       }

       public Fraccion(int x, int y) {

         num=x;
         den=y;
       }

       public static Fraccion sumar(Fraccion a, Fraccion b){

         Fraccion c=new Fraccion();
         c.num=a.num*b.den+b.num*a.den;
         c.den=a.den*b.den;
         return c;
       }

      public static Fraccion restar(Fraccion a, Fraccion b){

        Fraccion c=new Fraccion();
        c.num=a.num*b.den-b.num*a.den;
        c.den=a.den*b.den;
        return c;
      }
    
asked by Jose 21.04.2017 в 05:30
source

2 answers

1

If you're new to Java, I think the most useful explanation you can get is simply ... that you can. It is legal to do such a thing, to use the class that is being defined in own methods (or attributes). This is not particular to Java but to many languages (including, to some extent, in C ).

It is understandable that this is somewhat paradoxical (how can I use something that I have not yet finished defining ...?), but it is not so much when you think that in Java all reference of an object of a class is. a reference, which is something like an identifier number (a kind of handle or pointer).

For example, in the "internal mentions" to Fraccion here

public class Fraccion { ....
   public static Fraccion sumar(Fraccion a, Fraccion b){
      Fraccion c=new Fraccion();
      ...
   }
}

The compiler has no difficulty in understanding and implementing the above, even if "at that height still" (it is a saying) he does not fully know the structure of the class Fraccion . See more in detail how there is no problem with the chicken and the egg here, it would mean getting into details of implementation that, although interesting, may be out of place at a beginner level. All of us who learned Java starting by learning that it simply "can".

    
answered by 21.04.2017 в 07:14
0

First of all the class Fraccion was created to have a data type that contains a numerator and a denominator with which the fraction is represented. The class Fraction also offers methods that are addition and subtraction of fractions that is why the result of the operation must be returned as a type of data Fraction since this allows to save a numerator and a denominator. In java a class can have data that is of the same type as the class if it requires it.

    
answered by 21.04.2017 в 06:53