A1t
A1t

Reputation: 47

static or static final

i have an assignment about classes and objects. class is television, television objects will have attributes, some of them are constant

• manufacturer. The manufacturer attribute will hold the brand name. This cannot change once the television is created, so will be a named constant.

• screenSize. The screenSize attribute will hold the size of the television screen. This cannot change

i'm not sure of my declaration for the constant fields, i can't understand how they'll be constant should they be static final or final how do i guarantee that once the object is created value will not change

 class Television {

     private  final String MANUFACTURER;
     private final int SCREEN_SIZE;
     private boolean powerOn;
     private int channel ;
     private int volume ;

    public Television(String MANUFACTURER, int SCREEN_SIZE) {
        this.MANUFACTURER = MANUFACTURER;
        this.SCREEN_SIZE = SCREEN_SIZE;
        this.volume = volume;
    }

Upvotes: 1

Views: 1176

Answers (4)

Friso van Dijk
Friso van Dijk

Reputation: 669

You will want to use a final variables. Static means it's outside of the instance, so it should be the same for all objects regardless of the state of the object. It's associated with the class, not the object itself.

A final variable can be different per object, but is set only once and cannot be changed. This is what you're looking for: private final Object my_instance_final.

Upvotes: 0

Prashanth R
Prashanth R

Reputation: 181

Static keyword is not needed since the manufacturer and screen size differs television to television. I suppose you didn't forget to mention the volume in the constructor argument. Your code changes to follows.

 class Television {

 private final String manufacturer; // your declaration is correct as per your requirement but the naming convention is wrong final variables use lowerCamelCase instead of UPPERCASE.
 private final int screenSize;
 private boolean powerOn;
 private int channel;
 private int volume;

public Television(String manufacturer, int screenSize, int volume) {
    this.manufacturer= manufacturer;
    this.screenSize= screenSize;
    this.volume = volume;
}
}

There is no need to use static final keyword unless your declaring some universal constants for example say :

public static final float pi = 3.14;

whose value is same irrespective of the object or otherwise called instances.

Upvotes: 2

Adam
Adam

Reputation: 36703

I think you're confusing a variable being constant and a class attribute being immutable.

  • Constant usually refers to things that are constant throughout your program. A good example is Math.PI, these are declared static + final.
  • Being immutable is the concept an attribute that cannot be changed after object creation, this can be enforced using final, or simply declaring private and not providing a setter.

Your Television objects will likely have different manufactures and screen sizes, therefore you cannot use static as this would make all instances the same.

Also it is standard convention to only use CAPITALS_WITH_UNDERSCORES for constants, e.g. static + final.

So I'd declare your class as:

class Television {

    private final String manufacturer;
    private final int screenSize;
    private boolean powerOn;
    private int channel;
    private int volume;
    public Television(String manufacturer, int screenSize) {
        this.manufacturer = manufacturer;
        this.screenSize = screenSize;
        this.volume = volume;
    }
}

and maybe declare your manufacturers as constants elsewhere

public static final String SONY = "Sony";
public static final String SAMSUNG = "Samsung";

Upvotes: 2

Sridhar DD
Sridhar DD

Reputation: 1980

There are three forms of final variables:

  1. class final variables,
  2. instance final variables,
  3. local final variables.

There is no requirement to initialize a final variable at declaration but it must initialize before using it.

You can only initialize a final variable once.

class Program {
    /* Class Final Variables*/
    //Static means its common for all the objects you are creating like tv1, tv2.....
    static final int i1 = 10; 
    static final int i2;
    static {
        i2 = 10;
    }

    /*Instance Final Variables*/
    final int i1 = 10;
    final int i2;
    final int i3;
    {
        i2 = 10;
    }

    Program() {
        i3 = 10;
    }

    /*local final variable*/
    final int i1 = 10;
    final int i2;
    i2 = 10;
}

For more examples you can look into this where-can-final-variables-be-initialized

Upvotes: 1

Related Questions