create a variable of type class

2

Sorry if my question is wrong, but I have the following question, I am just learning java and I know the variable types are int double String , etc .. but I have noticed that you can create a variale of type 'name of the class', I would like to know what kind of data stores that, this case 'house'

package ejemplo;

public class casa {

**casa** tamaño;
}
    
asked by Paul Julio 21.04.2017 в 00:51
source

4 answers

7

As you mention there are the types int , double , string , but in the case of Casa is somewhat more complex.

To answer your question, it is best to clarify several things:

First you are defining a class Casa . This class can contain the types int , double , string , to define different attributes, for example like this:

public class Casa{
    string color;
    int cantidad_de_cuartos;
    bool tiene_garaje;
}

Then, this class as such can be used as a variable in the following way:

Casa miCasa = new Casa();

miCasa.color = "verde";
miCasa.cantidad_de_cuartos = 3;
miCasa.tiene_garaje = true;

In this example, my variable miCasa is of type Casa , but I interact with its attributes that are of type int , double , string . This is called an Object.

Also, my home object could be even more complex, you can have other objects inside, methods and other things that you need, but if I explain it here you get out of what you ask. :)

Then going back to your question what type of data does that store? , store what you want, the class you configure it as you want and you set what it will have, it does not have a specific data type as such .

    
answered by 21.04.2017 / 01:12
source
0

Class variables are variables whose values are the same for the class and for all its instances.

To indicate that a variable is a class variable, the keyword static is used in the declaration of the variables:

static tipoVariable nombreVariable;

How you can realize the typing is the same, the difference is that when using class variables these will be present since the object is created and not when it makes an instance of the object.

This link may help you

link

    
answered by 21.04.2017 в 00:54
0

I think what you're referring to is like declaring a class type variable, in what I understood, I put this example.

public class casa {

   int tamaño;
}

public class miscasas {

    casa micasa1;
    casa micasa2;

 public miscasas(){
 micasa1 = new casa();
 micasa2 = new casa();
 micasa1.tamaño = 30;
 micasa2.tamaño = 50;
}
    }

In this example we have the class house with the attribute type size integer and the class miscasas with the attributes, micasa1 and micasa2 both house type, both objects for being house type have the attribute size and assign a size to each one, They are the same type of object but the attributes with different values.

    
answered by 21.04.2017 в 01:03
0

This type of data is known as TDA (abstract data types) They are data that are always composed of primitive data or even data of type Object and that the programmer creates to meet their needs.

For example, someone can make a house with the following values:

public class Casa{
int largo;
int ancho;

public Casa(int largo, int ancho) {
this.largo=largo;
this.ancho=ancho;
} 
} 

But another programmer may be interested in other information about a house:

    public class Casa {
    String color;
    Persona habitantes[];

    public Casa(String color, Persona habitantes[]) {
    this.color=color;
    this.habitantes=habitantes;
    } 

} 

In the first case the house is only composed of primitive data types, while in the second case the house is composed of data of type Object.

    
answered by 21.04.2017 в 03:58