Java defining or initializing attributes of a class
Asked Answered
R

3

23

Is there a difference between defining class attributes and initializing them? Are there cases where you want to do one over the other?

Example:

The following code snippets should point out the difference that I mean. I'm using a primitive and an object there:

import Java.util.Random;

public class Something extends Activity {
    int integer;
    Random random = null;

    Something(){
        integer = 0;
        random = new Random();
        ....

vs.

import Java.util.Random;

public class Something extends Activity {
    int integer = null;
    Random random;

    Something(){
        integer = 0;
        random = new Random();
        ....
Rind answered 9/7, 2013 at 4:0 Comment(2)
I would list the variables at the class level and initialize them with a constructor.Cardew
also you're trying to pass a null to a primitive which could cause some headaches.Cardew
T
22

Firstly you cannot set a primitive to be null as a primitive is just data where null is an object reference. If you tried to compile int i = null you would get a incompatible types error.

Secondly initializing the variables to null or 0 when declaring them in the class is redundant as in Java, primitives default to 0 (or false) and object references default to null. This is not the case for local variables however, if you tried the below you would get an initialization error at compile time

 public static void main(String[] args)
 {
     int i;
     System.out.print(i);
 }

Explicitly initializing them to a default value of 0 or false or null is pointless but you might want to set them to another default value then you can create a constructor that has the default values for example

public MyClass
{
   int theDate = 9;
   String day = "Tuesday";

   // This would return the default values of the class
   public MyClass()
   {
   }

   // Where as this would return the new String
   public MyClass (String aDiffDay)
   {
      day = aDiffDay;
   }
}
Tinderbox answered 9/7, 2013 at 4:32 Comment(0)
S
2

Shanku and Morpheus answered the question correctly. First, you will get a compile error setting your primitive int variable "integer" to null; you can only do that for Objects. Second, Shanku is right that Java assigns default values to instance variables, which are "integer" and "random" in your example code; instance variables are viewable within the class or beyond depending on the scope (public, private, protected, package).

However, default values are not assigned for local variables. For instance, if you assigned a variable in your constructor like "int height;" then it will not be initialized to zero.

I would recommend reading the Java variable documentation, which describe the variables very well and furthermore you could also look over the Java tutorials, which again are great reading material.

Stilu answered 9/7, 2013 at 4:34 Comment(0)
M
1

In Java, initializing is defined explicitly in the language specification. For fields and array components, when items are created, they are automatically set to the following default values by the system:

numbers: 0 or 0.0

booleans: false

object references: null

This means that explicitly setting fields to 0, false, or null (as the case may be) is unnecessary and redundant.

Mixtec answered 9/7, 2013 at 4:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.