Win a copy of Kotlin Cookbook this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Local Variables page 29 gives a different compile error than what the book shows

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I am using the Oracle Certified Associate Java SE 8 Programmer I book by Boyarsky and Selikoff. I am on page 29 of the book under Local Variables. The example the book provides is below:-



on page 30 of the book it states that the compiler generates an error on line 7 such as below:-
 Test.java:7: variable x might not have been initialized
   int reply = x + y;
           ^

However, when i try to compile a file like above i get the below error instead:-
Local.java:4: error: illegal start of expression
 public int notValid() {
 ^
Local.java:15: error: class, interface, or enum expected
}
^
2 errors

My code looks similar to the one above except that i added the public static void main(String[] args) { in the code.


I do understand why one should get a compile error on line 7 as the book states. It is because the variable x is not initialized before it is used in the expression on line 7. However, why am i NOT getting the same error? I am getting a totally different error and that too on line 4 of my code and somewhere around here : public int notValid() { . Even when i initialize x by giving it a value, my code doesn't compile. Any help would be appreciated. Thanks.
 
Saloon Keeper
Posts: 11022
244
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You declared a method inside a method. That's not valid Java code.
 
aku pat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You declared a method inside a method. That's not valid Java code.



Okay so i removed the main method and the code compiles however when i try to run the code after initializing the variable x with a value of 5, it doesn't run and i get the error below:-

Error: Main method not found in class Local, please define the main method as:
 public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

^Hence, the reason why i included the main method inside a method notValid. My code is as below:-

 
Marshal
Posts: 67050
255
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are unrelated errors; you need to run a main method with a particular heading to start the program, but you won't notice that until runtime. The fact that x wasn't initialised is a compile time error, and it appears earlier.
 
aku pat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Those are unrelated errors; you need to run a main method with a particular heading to start the program, but you won't notice that until runtime. The fact that x wasn't initialised is a compile time error, and it appears earlier.



Yes and I understand why x is a compile time error. My issue is understanding why even after x is initialized (ex: giving int x a value of 5) it doesn't run when i type in java Local after compiling it successfully using javac Local.java and then it creates Local.class without any compile errors. However, i just can't seem to run the code when doing java Local. My code is below. It would really help if anyone can tell me why the code won't run or show me how to run it instead. Thanks.

Code is below:-

 
Saloon Keeper
Posts: 6619
62
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a) To run something you need a main() method.
b) You can't define a method inside of another method.
 
Carey Brown
Saloon Keeper
Posts: 6619
62
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To elaborate:
In order to call your notValid() method, main() would have to create an instance of "Local", and then use that instance to call notValid(), and presumably, do something with the return value.
 
Carey Brown
Saloon Keeper
Posts: 6619
62
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Note: Proper indentation will help you find some of your errors.
 
Campbell Ritchie
Marshal
Posts: 67050
255
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

aku pat wrote:. . . doesn't run when i type in java Local after compiling it successfully using javac Local.java and then it creates Local.class without any compile errors. . . .

That code you posted doesn't compile. You may have an old version of Local.class, before the latest compilation attempt. Please verify the dates and times of the .java and .class files.
 
aku pat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:
Note: Proper indentation will help you find some of your errors.



Hi Carey,

Thanks for your reply. Yes, so your code compiles and also runs without an error. So you need main() method to run an instance of the Local class (I'm guessing JVM compiles it to bytecode .class) and you cannot have a method inside a method. This works but I am so bummed that I can't see the value 15. I have tried using . However, i get a void can't be converted to int.
It makes sense since System.out.println is of type void and reply is returning an int. Hence, why it won't work. So on line 12 i write this code to catch it before the ending braces } for the Local class
and doing this give me the error below:-
Local.java:13: error: <identifier> expected
System.out.println(value);
                 ^
Local.java:13: error: <identifier> expected
System.out.println(value);
                       ^
2 errors

Below is my code:-


I really want to see the value of 15 show up when i run the code using java Local. Is it possible?

 
Carey Brown
Saloon Keeper
Posts: 6619
62
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method names should clearly indicate what the method is doing. "notValid" makes it sound like the method is checking something for validity, or more specifically, that something is not valid. As such, I would be expecting it to be returning a boolean and not an int. Based on the code in the body of the method it could be described as "sumXandY", and could be even more descriptive if we knew what X and Y represented. So, I think you need to reconsider your method name.
 
aku pat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

aku pat wrote:. . . doesn't run when i type in java Local after compiling it successfully using javac Local.java and then it creates Local.class without any compile errors. . . .

That code you posted doesn't compile. You may have an old version of Local.class, before the latest compilation attempt. Please verify the dates and times of the .java and .class files.



You're right it doesn't compile. I deleted the old Local.java and Local.class file. Then created a new Local.java file and re-ran the below code and it correctly gave me this error:-
○ → javac Local.java
Local.java:3: error: illegal start of expression
public int notValid(){
^
Local.java:10: error: class, interface, or enum expected
}
^
2 errors



 
Carey Brown
Saloon Keeper
Posts: 6619
62
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You normally can't have code like this floating about your class, these lines should be contained inside of a method (hint: main()).

notValid() is an instance method, you cant call it without giving it some instance to operate on. Exampe: someInstanceOfTheLocalClass.notValid()
 
aku pat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Method names should clearly indicate what the method is doing. "notValid" makes it sound like the method is checking something for validity, or more specifically, that something is not valid. As such, I would be expecting it to be returning a boolean and not an int. Based on the code in the body of the method it could be described as "sumXandY", and could be even more descriptive if we knew what X and Y represented. So, I think you need to reconsider your method name.



Hi Carey, again thanks for your reply. I totally understand and agree that method names should clearly indicate what the method is doing. However, I am just following the code example as described verbatim from the book. On page 29 of the OCA book by Boyarsky and Selikoff, the authors state, "A local variable is a variable defined within a method. Local variables must be initialized before use. They do not have a default value and contain garbage data until initialized. The compiler will not let you read an uninitialized value. For example, the following code generates a compile error:


The code above is exactly from the book. I understand why line 4 above does not compile. Because x is not initialized. I 100% get this. However, shouldn't the code compile once x is initialized? Yes, i think it should. so on line 3 from the same code above all i do is write . And then i save it and compile it but it won't. It gives me an error. That is my entire issue. I want to see the number 15 on the output when i run java Local after i can somehow get it to compile using javac Local.java. That's all. I guess I should just move on but it bums me that I can't get it to compile when I personally initialize the variable x with type integer with a value of 5.
 
aku pat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:You normally can't have code like this floating about your class, these lines should be contained inside of a method (hint: main()).

notValid() is an instance method, you cant call it without giving it some instance to operate on. Exampe: someInstanceOfTheLocalClass.notValid()



Wow, thank you so much. I took your advice and ran the code below. It worked and I saw 15 on the output! Thank you!



So, you can call an instance of a class (object) inside the same class where it is defined? I mean the code works but I just don't know why. Only thing I know is what you said, "You can't call it without giving it some instance to operate on".

 
Carey Brown
Saloon Keeper
Posts: 6619
62
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

aku pat wrote:"You can't call it without giving it some instance to operate on".


main() is a static method, therefore it is NOT called with an instance of the class.

notValid() is NOT static, therefore it REQUIRES an instance when it is called. An instance can come in the form of a reference variable that refers to an instance. Or IF you happen to be calling your instance method from inside another instance method, then you also have the option of calling it explicitly or implicitly using "this".
 
aku pat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

aku pat wrote:"You can't call it without giving it some instance to operate on".


main() is a static method, therefore it is NOT called with an instance of the class.

notValid() is NOT static, therefore it REQUIRES an instance when it is called. An instance can come in the form of a reference variable that refers to an instance. Or IF you happen to be calling your instance method from inside another instance method, then you also have the option of calling it explicitly or implicitly using "this".



Thank you Carey for this explanation!
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!