3

I've written the below code in a text file and saved it as Exercise1.java:

import java.util.*;
public class Exercise1
{
 public static void main(String[] args)
 {
  char myChar;
  int myInt;
  System.out.println(myChar);
  System.out.println(myInt);

 }
}

then in cmd I write

javac D:\Projects\Exercise1.java

I get 2 errors both saying the declared variables might not be initialized. But if I initialize the variables the above code generates the "Exercise1.class" file. And then when I write

java D:\Projects\Exercise1

I get java.lang.ClassNotFoundException. Now what I wonder are:

  1. Aren't primitive types initialized to default values in Java (I thought and read they did).
  2. Why can't I run my program and get the Exception as there do not not seem to be any mistakes?
2
  • have you used the comment Java Exercise1 to run the program Commented Jul 29, 2011 at 5:29
  • Member variables are initialized to their default values (0, false, null). However, the variables in your example are local variables, which you are required to initialize before referencing. Commented Jul 29, 2011 at 5:36

6 Answers 6

5

The java command doesn't take a filename - it takes a classname. The name of your class is just "Exercise1" - if it were in a package, it might be something like "foo.bar.Exercise1". You then separately specify the classpath to tell the JVM how to find classes, e.g.

java -cp D:\Projects Exercise1

... or you can change directory and take advantage of the fact that without a classpath specified, the JVM will include the current directory in the classpath:

cd D:\Projects
java Exercise1

EDIT: I'd assumed you'd already fixed the errors in the code. Don't try to run the code before it compiles without errors. The errors are due to your uninitialized local variables. Only instance and static variables are given default values.

Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately that did not work either. And aren't primitive types initialized to default values even if we don't explicitly initialize them?
@Mikayil: Not for local variables, no. Were you trying to run this code before managing to get it to compile without errors? That was never going to work...
-cp overrides any defined CLASSPATH environment variable
2
cd D:\Projects
java -cp . Exercise1

Comments

2

Not sure how you compiled your program, it should failed at the two following lines:

 char myChar;
 int myInt;

Because you haven't given any values.

Then, although you can execute this line to compile:

javac D:\Projects\Exercise1.java

If you do not change the classpath, you should be in the Projects folder, and then run

java Exercise1

so it prints the values of myChar and myInt

2 Comments

But aren't primitive types initialized to default values in Java?
I think it will fail at the fist use of each variables (System.out.println(...);, not at it declaration.
1

@Jon : He hasn't declared any packages as of now.

The most common problem is that classpath issue. Try changing the path to the said directory and then compile. Or try changing the class path from set classpat=address_of directory

You have to initialize a "local" variable else java will give error. Java does provide the default value to the variables but not for local variables http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Comments

1

to answer your first point:

Java does initialized to the default values only if the object of a class is instantiated. Since here you are not instantiating a class, they are not initialized. The following code will initialize the variables to the default values:

class Exercise{
    int myInt;
    char myChar;
    }

class Exercise1{
public static void main(String []a){
//Instantiating Exercise
Exercise e=new Exercise();
System.out.println(e.myInt);
System.out.println(e.myChar);
}
}

Here i have not initialized the variables but i have instantiated the exercise class. On instantiation, the values of the variables are set to the default values. If you are new to java and you are not familiar with classes, i suggest you read up some good books on Java.

To answer your second question, you can add your CLASSPATH and PATH to the environment variables. To do this go to system properties->advanced->environment variables.

Comments

0

After changing my local variables to static instance variables I could both able to get class file without having to explicitly initialize them to some values and run the program and get the default values printed. Thank you all for you effort.

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.