Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can´t I create a class object within a method from a different class in Java?

I tried something in my code and it didn´t work (the error when compiling was "The local variable fundo is never read"). I´ve made some changes and it worked, but I would like to know why it didn´t work in the first place.

I have a class called Setor, in my code I´m trying to create an object from that class in the class Vendedor

Here is my first code:

class Vendedor{

  void abreTeatro(int codigoCamarote, int capacidadeCamarote, int precoCamarote, int codigoFrente, 
              int capacidadeFrente, int precoFrente, int codigoMeio, int capacidadeMeio, int precoMeio, int codigoFundo,
              int capacidadeFundo, int precoFundo)
  {
     Setor camarote = new Setor(codigoCamarote, capacidadeCamarote, precoCamarote);
     Setor frente = new Setor(codigoFrente, capacidadeFrente, precoFrente);
     Setor meio = new Setor(codigoMeio, capacidadeMeio, precoMeio);
     Setor fundo = new Setor(codigoFundo, capacidadeFundo, precoFundo);  

  };
 }

If I create the objects outside the method it works as you can see below:

class Vendedor{

  Setor camarote;
  Setor frente;
  Setor meio;
  Setor fundo;


  void abreTeatro(int codigoCamarote, int capacidadeCamarote, int precoCamarote, int codigoFrente, 
              int capacidadeFrente, int precoFrente, int codigoMeio, int capacidadeMeio, int precoMeio, int codigoFundo,
              int capacidadeFundo, int precoFundo)
  {
     camarote = new Setor(codigoCamarote, capacidadeCamarote, precoCamarote);
     frente = new Setor(codigoFrente, capacidadeFrente, precoFrente);
     meio = new Setor(codigoMeio, capacidadeMeio, precoMeio);
     fundo = new Setor(codigoFundo, capacidadeFundo, precoFundo);  

  };
 }

Here is the Setor class:

public class Setor
{

  int _codigo;
  int _capacidade;
  int _preco;

  public Setor (int codigo, int capacidade, int preco){
  _codigo = codigo;
  _capacidade = capacidade;
  _preco = preco;

  System.out.println(_codigo + " " + _capacidade + " " + _preco);
  };
 }

I would like to know what´s wrong with my first try.

Also, I might be using some terminology wrong. I´m sorry for that, edits are very welcome!

like image 708
user3347814 Avatar asked May 27 '14 18:05

user3347814


People also ask

How to create multiple classes from one class in Java?

Let one class have the main () method and instantiate (create object) other classes inside that Main class (the class that contain the main method). That way you get to run the code once.

How to create objects from a class in Java?

Since many houses can be made from the same description, we can create many objects from a class. We can create a class in Java using the class keyword. For example, Here, fields (variables) and methods represent the state and behavior of the object respectively. In the above example, we have created a class named Bicycle.

What is a class in Java?

A class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order: Modifiers: A class can be public or has default access (Refer this for details).

How to use main class and its methods in Java?

5) In order to use the Main class and its methods, we need to create an object of the Main Class. 6) Then, go to the main () method, which you know by now is a built-in Java method that runs your program (any code inside main is executed).


2 Answers

The difference is in where the variables you are populating get declared.

What your first example is doing is creating local variables. They go out of scope when the method completes. Nothing is referring to them anymore and they eventually get garbage-collected.

In the second example you are setting instance variables (where the declarations are located within the curly braces after the class name, and not within a method or constructor definition), so the objects stick around after the method call is through.

like image 77
Nathan Hughes Avatar answered Oct 21 '22 22:10

Nathan Hughes


It is the scope of the variable. In the first example, you have created local variables which is visible only within that method. Whereas in the second example, you have created instance variables which are visible to all the methods in the class.

Local variable - created in stack and vanishes once the method exits. Instance variable - created in heap which exists until there are no more references to it

like image 40
Gita Avatar answered Oct 21 '22 21:10

Gita