How to create a class in java?

4

I would like to know how to create a class in java , how to use this class in the main program and how the variable created in that class is.

    
asked by anonymous 04.06.2015 / 18:49

4 answers

5

Initially the principal method of the main class (program start) is called, but no instance of the class (object) is created just by doing so.

To create an instance of a class, it is necessary to call the constructor of this class (if there is not one, the compiler will create a default constructor without parameters).

After instantiation you can assign values to the attributes of the object, or pass them as parameters to the constructor and assign them to the constructor.

In this assignment one must be careful, because depending on the class of the object being used (in this example the main class), a compilation error will occur if there is an attempt to violate the visibility rules.

The attribute modifiers, constructors, and methods are: protected , package (also called default , which is the absence of modifier), private and public . >

The only difference from protected to package (no modifier) is that protected allows subclasses (heirs classes) from outside the package to have access.

More details on this SOen

Note: In Java 8, the keyword "default" is a valid modifier for interface methods, and in that context it has no relation to package visibility (default, absence of modifier).

class Programa {
  public static void main(String[] args) {
    Conta minhaConta = new Conta();

    minhaConta.dono = "Duke";
    minhaConta.saldo = 1000.0;

    System.out.println("Saldo atual: " + minhaConta.saldo);
  }
}

Class counts with its methods and attributes:

class Conta {
   double saldo;
   String dono;

   void saca(double quantidade) {
     double novoSaldo = this.saldo - quantidade; 
     this.saldo = novoSaldo;
   }
 }
Complementing the subject of modifiers: To ensure greater security and greater encapsulation (among other characteristics) it is necessary to restrict access to variables (usually using access modifiers protected and private ) making it impossible to directly access the attribute ( check the access level of each modifier), to provide access Access Methods get and set :

private double x; 

public double getX() { 
      return x; 
} 

public void setX(double x) { 
    this.x = x; 
}

For example, it is always possible to validate the values to be assigned.

    
04.06.2015 / 18:57
5

The classes that can be called as main for Java-SE applications are classes that serve as the entry / exit point of your publication, these classes are those that have the public and static method main , this method expects to receive a array of String s representing the arguments offered when the class was called with the Java command from the command line.

Even if you use an IDE to run your application, the IDE will call this method informing you of the parameters that can be passed through the execution parameter screen.

In the body of the main method you can reference static variables in your class, instantiate the class itself through one of its constructors, and then reference its variables if they are visible, which will depend on their scope. You can also instantiate other classes, manipulate visible variables, and call their visible methods according to scope.

@Rodolfo, from my point of view your question is very broad and may involve a lot of knowledge regarding building classes and small programs in java.

    
04.06.2015 / 19:25
4
class NomeDaClasse {

      // Atributo
      public int atributo;

      // Método construtor
      public NomeDaClasse() {
      }

      // Metodo
      public void metodo() {

      }
}

To create an instance of the class:

NomeDaClasse objeto = new NomeDaClasse();
    
04.06.2015 / 18:58
2
class AlgumaCoisa 
{
   /* Alguma variável qualquer */
   bool ClasseLegal = true;

   int Arg1 = 0; //primeiro argumento
   int Arg2 = 0; //segundo argumento

   /* O método construtor deve ser o nome da classe */
   public AlgumaCoisa(int Argumento1, int Argumento2) //Pode remover os argumentos se quiser
   { Arg1 = Argumento1;
     Arg2 = Argumento2; }

   /* Propriedade somente leitura qualquer */
   public readonly int CodigoDaClasse()
   { return 20; }

   public int SomaDeles() { return Arg1 + Arg2; }
   public int MultDeles() { return Arg1 * Arg2; }
   public int DivsDeles() { return Arg1 / Arg2; }
   public int SubtDeles() { return Arg1 - Arg2; }
}

to declare it use:

AlgumaCoisa nomeAqui = new AlgumaCoisa(ValorDoArgumento1, ValorDoArgumento2)

Here's an example with this class:

AlgumaCoisa nomeAqui = new AlgumaCoisa(10, 10)
int Soma          = nomeAqui.SomaDeles(); // 20
int Multiplicação = nomeAqui.MultDeles(); // 100
int Divisão       = nomeAqui.DivsDeles(); // 1
int Subtração     = nomeAqui.SubtDeles(); // 0
    
05.06.2015 / 01:28