doubt on how a class is structured in java

5

I am just beginning to learn Java and instead of reading 20 documents with 200 pages each, I would like to ask a simple question;

what are the methods that can be used in a java class?

For example:

A. A constructor (has the same name of the class)

public class Clase {
    public Clase() {
        //código aquí
    }
}

B. Method with void (does not return anything)

public class Clase {
    public void algo() {
        //código aquí
    }
}

C. Method with return (the heading of the method carries the type of data that returns)

public class Clase {
    public (tipo de valor) algo() {
        //código aquí
        retorno
    }
}

D. Main method, to run the Java program

public class Clase {
    public static void main(String s[]) {
        //código aquí
    }
}
    
asked by virtual8870 10.01.2018 в 15:53
source

2 answers

1

I would recommend that you read only ONE manual that explains the Java syntax, because a summary is not going to be of great help to you as a reminder of what you should know, but here it goes:

In a class you can define static (class) or instance methods, in addition to the constructor, which always returns an object of the class:

class MiClase {
     MiClase() {}
     void método(Param p1) {...}
     static void métodoEstático(Param p) {...}
     static int métodoEstático2() { return 0;}
}

If the class is abstract, you can define abstract methods, without implementation for the child classes to implement them:

abstract class MiClase {
     void abstract métodoAbstracto(Param p1);
     int abstract métodoAbstracto2();
     void métodoNormal() {...}
}

In an interface all methods are abstract unless you want to define a default implementation:

interface MiInterfaz {
   String metodo1(); //abstract se puede omitir
   default String método2() { return "";}
}

And this is all important. There are modifiers final (methods that can not be overwritten, come to be the opposite of abstract ), private , protected and public , which indicate who can use these methods, but do not affect the execution itself.

    
answered by 10.01.2018 в 16:49
1

Answering your question what are the methods that can be used in a java class? and how it is structured.

In java the basic structure of a class usually follows the following scheme:

  - attributes   - Builders   - Getters (accessors).   - Setters (mutators).   - class methods.

With regard to your question what are the methods that can be used in a java class?

The essential methods when implementing your Java class and that are essential but not mandatory are:

  - Builders   - Getters (accessors).   - Setters (mutators).

In code the basic structure would be as follows:

 public class Persona(){
    //Atributos de la clase 
    private String nombre;
    private int edad;

    //Constructores

    //Constructor Vacio
    public Persona(){
    }

    //Constructor con Parametros
    public Persona(String nombre,int edad){
     this.nombre = nombre;
     this.edad = edad;
    } 

    //Metodos accesadores y mutadores

    public string getNombre(){
       return nombre;
    }


    public void setNombre(String nombre){
       this.nombre=nombre;
    }


    public string getEdad(){
       return edad;
    }


    public void setEdad(int edad){
       this.edad=edad;
    }




 }

To make it clearer we will analyze the code:

The first thing we do is declare the attributes of the class, it is recommended declare the attributes at the beginning of the class.

After we implement the constructors, you can implement as many builders as you need, this is called overload. , and you will ask what the builders will do for us? will serve to instantiate our classes or our objects.

Finally we have the accessor and mutator methods ( getters and setters ) that are the encapsulation of the declared attributes.

Then the getters or get, will allow us to obtain and show what our declared variable has and the setter or set will allow us to modify the value that our variable has.

The class methods you can implement are the ones you need. there is no maximum or limit of methods.

I would recommend studying a bit what is encapsulation, polymorphism, object-oriented programming, interfaces, inheritance is the most basic thing you will use when you work in java.

    
answered by 10.01.2018 в 16:23