-2
Guys I started studying java and I have a question about the main method, I have a project to do and I would like to do if in the main method I can by functions and other methods as for example register users and etc.
-2
Guys I started studying java and I have a question about the main method, I have a project to do and I would like to do if in the main method I can by functions and other methods as for example register users and etc.
2
Java constructors
A Java constructor is a special method used to initialize objects. The constructor is called when an object in a class is created. It can be used to define initial values for object attributes. Note that the constructor name must match the class name and cannot have a return type (as void) and the same is called when the object is created.
All classes have constructors by default: if you don’t create a class constructor, Java will create one for you. However, you cannot set starting values for object attributes.
Parameters of the constructor
Constructors can also use parameters, which are used to initialize attributes.
NOTE: You can have as many parameters as you want.
Follow a basic example:
// Criando a Classe denominada MyClass
//CLASSE
public class MyClass {
int x; // Criando o atributo da classe
// Criando o construtor da classe MyClass
public MyClass() {
x = 5; // Definindo o valor inicial do Atributo X da Classe
}
}
// MAIN
public static void main(String[] args) {
MyClass myObj = new MyClass(); // Criando o objeto da classe MyClass (Isso vai chamar o construtor)
System.out.println(myObj.x); // Exibindo no Console o valor do atributo X da classe MyClass
// Outputs 5
}
We can also start the class with other attribute types and even more, other class instances:
//CLASSE
public class MyClass {
String nome = nome;
private int x; // Criando o atributo da classe
public double y;
protected long z;
private final MyClassTwo myClassTwo;
// Criando o construtor da classe MyClass
public MyClass(int x, int y, int z) {
this.x = x; // Atribuindo o valor a variável, quando instancia o objeto da classe "new"
this.y = y; // Atribuindo o valor a variável, quando instancia o objeto da classe "new"
this.z = z; // Atribuindo o valor a variável, quando instancia o objeto da classe "new"
this.myClassTwo = new MyClassTwo(); // Instanciando uma outra classe dentro da classe MyClass
this.nome = "MINHA CLASSE!" // Definindo o valor inicial do Atributo X da Classe
}
}
// MAIN
public static void main(String[] args) {
MyClass myObj = new MyClass(1, 2, 3); // Criando o objeto da classe MyClass (Isso vai chamar o construtor)
System.out.println(myObj.nome);
System.out.println(myObj.x);
System.out.println(myObj.y);
System.out.println(myObj.z);
System.out.println(myObj.myClassTwo);
}
NOTE:
Read also about
Browser other questions tagged java oop
You are not signed in. Login or sign up in order to post.
I just answered your question, but I suggest you read the community guidelines of how to ask a good question.
– user148754
In the title you mention constructor, but in the question you talk about the main method (it would be the
main
?) If it’s about a constructor, read https://answall.com/q/73530/112052 - Anyway, it’s important to [Edit] the question and make your question clearer– hkotsubo
You can call any type of functions within your "main" class, but what is recommended is that you separate the responsibilities of your project so that each class has a unique responsibility. Ex: if Voce has a private method in another class (public user cadUser), it has the function of registering a user. You can call him in his main class without problems. BOM.... I don’t know if that’s what you want, but that’s what I meant by your question. I suggest you read some book on OO https://www.caelum.com.br/apostilas
– Rodrigo Nantes