简体   繁体   中英

Difference between attribute and variable / data types etc. (java)

I don't understand those terms given above fully. So I would be very happy, if someone could correct me and explain me the differences.

There are data types like int, double, boolean etc. those are pretty much clear and need no further explanation. You can or should also a part in front of them to sign their visibility like public, private, final, static etc. their properties can also be looked up.

I think attributes of a class can be viewed the entries of a struct in C++, for the example the properties of an object, what it is made of etc., you give the class some data types to describe it; in fact it is nothing magical, but rather something very simple. Is this right?

Both constants or variable can be as attribute(filed) of a class. It is a part of the class to describe what the class has, from which we can figure out what the status of the instance of the class is at(for example:how old is a man).

Attributes are typically can be accessed through some getter or setter method though we can also define the attribute to be public.

Check the below example:

public class Human{

    //now, I define three attributes for the class Human 
    //two of them are variable 
    private int age;
    private String name;

    //another one is a constant
    public final boolean isIntelligent=true;

    public int getAge(){
       return age;
    }

    public int getName(){
       return name;
    }


    public void sayHello(){
       //here hello is a variable, means it can be assigned a value for many times
       String hello="";
       if(age>3){
           hello="hello world"//we assign "hello world" to this variable 
       }
       else{
           hello="awuawuau";//baby does not know hello, so we assign another one 
       }

      System.out.println(hello);
    }
}

So, you can consider that the variable or constant which belong to a class is a attribute(field),which typically can be accessed in some way, while a variable(if it is not an attribute) typically will be in a method cannot be accessed outside , when the method ends, the variable will be gone.

An attribute is another term for a field. It's typically a private constant or a private variable that can be accessed directly (they can also be public). For example, in an array in Java it is actually an object and you are accessing the public constant value that represents the length of the array.

public class Player 
{ 
private String name="Luigi"; 
private int score=0; 
} 

name and score are attributes of the the class player

A variable is a container that holds values that are used in a Java program. Every variable must be declared to use a data type. For example, a variable could be declared to use one of the eight primitive data types: byte, short, int, long, float, double, char or boolean. And, every variable must be given an initial value before it can be used.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM