0

I got an error compiling the following code:

class TapeDeck 
{
    boolean canRecord = false;

    void playTape() 
    {
        System.out.println("tape playing");
    }

    void recordTape() 
    {
        System.out.println("tape recording");
    }
}

class TapeDeckTestDrive
{
    public static void main(String [] args)
    {
        TapeDeck t; 
        t.canRecord = true;
        t.playTape();

        if(t.canRecord == true) 
        {
            t.recordTape();
        }
    }
}

The error is:

TapeDeck.java:21: error: variable t might not have been initialized
        t.canRecord = true;
        ^
1 error

How shall I initialize the variable t in the class?

1
  • You are missing the call to the object's constructor for instantiation. TapeDeck t = new TapeDeck(); Commented Jun 20, 2015 at 20:30

6 Answers 6

3

Use new keyword to initialize objects.

TapeDeck t = new TapeDeck();
Sign up to request clarification or add additional context in comments.

Comments

1

you can alter the code as:

 public class TapeDeck 
        {
           // code
        }

       public class TapeDeckTestDrive
        {
            public static void main(String [] args)
            {
                TapeDeck t = new TapeDeck(); 
                t.canRecord = true;
                t.playTape();

                if(t.canRecord == true) 
                {
                    t.recordTape();
                }
            }
        }

Comments

1

Instantiate your TapeDeck object

class TapeDeckTestDrive
{
    public static void main(String [] args)
    {
        TapeDeck t = new TapeDeck(); 
    ...

Comments

1

You need to create instance of TapeDeck and assign it to t reference. You can create instance using new keyword like

TapeDeck t; 
t = new TapeDeck();

or in one line

TapeDeck t = new TapeDeck();

Otherwise t stays uninitialized and you can't call any method on uninitialized variable.


BTW if(t.canRecord == true) is considered as code smell since it can lead to problems like if(variable=true) where we assign true to variable (=true) instead of comparing (==true). To avoid such problems simply use

if(t.canRecord)

or if you feel like having == and true increases readability so you don't want to remove them use yoda-condition

if(true == t.canRecord)

Comments

1

You can initialize using new and the name of your class, in your case TapeDesck();.

TapeDeck t = new TapeDeck();

If you don't do that you won't be able to use any of the methods inside of your class TapeDesck().

If you have a constructor, then you can initialize it with it. For example, if you have this constructor (it's just an example, don't focus on the name of the variables):

class TapeDesck
{
   private int number;
   private String prove;

   public TapeDesck(int number, String prove)
   {
       this.number = number;
       this.prove = prove;
   }
}

You can initialize it like this:

TapeDeck t = new TapeDeck(1,"This is a prove"); 

I expect it will be helpful for you!

Comments

0

You just declared the variable on the 1st line in your main():

TapeDeck t;  // Variable just declared not initialized.  

Solution:

TapeDeck t = new TapeDeck();  // Initialize it to use members.  

You seem to be from C++ background. This kind of initialization of objects are allowed in C++ but not in Java. You strictly have to use the new keyword to initialize an object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.