• Post Reply Bookmark Topic Watch Topic
  • New Topic

Core Java Basic  RSS feed

 
Prakash Rai
Ranch Hand
Posts: 107
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Q1. By what name Should i save this program and why?


 
Misha van Tol
Ranch Hand
Posts: 56
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A.java, so the JRE can find the main method and knows where to start. But the B class is going to give you an error like this. Since you've declared the B class to be public it should be in it's own file called B.java. If you remove 'public' it will compile and run.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember this is the very very basic of Java. you should NOT miss these facts!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this question is not so cut-and-dry. A given .java file can only have ONE public class, and the file name has to match that class name (including case). So, by that rule, since "B" is your public class, the file should be called "B.java".

However, I'm assuming you'll be wanting to run "java A" to run A's main method...which will cause you other issues if 'B' is the public class.

In other words, the question is not clear. If your only goal is to save it, it doesn't matter what you call it.
if your goal is to save it and compile it, "B.java" would do the trick. It will compile, but you won't be able to run anything.
If your goal is to save it, compile it AND run it, you'll need to do more than JUST give it the right file name.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect explanation fred
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could save the file as B.java (which you must, if B is the top-level public class in that file), then compile as...

javac B.java

...and run as...

java A

...since A is the class containing main.

But I hope it's clear this is probably not the best approach.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[campbell@campbell1110 ~]$ cd java
[campbell@campbell1110 java]$ gedit B.java&
[1] 2544
[campbell@campbell1110 java]$ javac A.java
javac: file not found: A.java
Usage: javac <options> <source files>
use -help for a list of possible options
[1]+ Done gedit B.java
[campbell@campbell1110 java]$ javac B.java
[campbell@campbell1110 java]$ java A
The value of a :30
[campbell@campbell1110 java]$
. . . which shows Fred and Marc W were correct.

But "A" and "B" are poorly-chosen names for files.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah...i see what I did...I compiled it with "javac B.java", which worked, but then brain-farted and tried running 'javac A' - which gave me a weird error.

duh...
 
Prakash Rai
Ranch Hand
Posts: 107
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Q1. Why we can not assign final variable inside any method other than constructor?
Q2. Is it always necessary to assign final variable? Suppose if i have't initialize final variable then why it is not taking default value.?
When I removed this this multiple line comments then It was giving error.
 
Prakash Rai
Ranch Hand
Posts: 107
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:ah...i see what I did...I compiled it with "javac B.java", which worked, but then brain-farted and tried running 'javac A' - which gave me a weird error.

duh...
Is it necessary to give declare public class for which contain main() method. or we can can declare any class as a public but that should be saved be Public class name. please clear me me this point.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very good questions


Somprakash Rai wrote:
Q1. Why we can not assign final variable inside any method other than constructor?



1. Because, that variable is final. A variable declared as a final should be initialized within the initialization period of its associated object (instance). An initialization period of an object (instantiation process) is said to be completed in Constructor. Thats why you use new to instantiate. So till that time you are allowed to postpone the initialization of the variable.

Generally we initialize the value of a final variable during the declaration itself. Otherwise, we go with constructors. If not through any of these, the compiler would complain saying that you are violating the contract of the final!

Somprakash Rai wrote:
Q2. Is it always necessary to assign final variable? Suppose if i have't initialize final variable then why it is not taking default value.?
When I removed this this multiple line comments then It was giving error.


Yes of course. You have to assign a value. If you think you can assign the value later, the variable will no longer be of final. The answer is interlinked with the previous answer.

Hope this helps!
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somprakash Rai wrote:
fred rosenberger wrote:ah...i see what I did...I compiled it with "javac B.java", which worked, but then brain-farted and tried running 'javac A' - which gave me a weird error.

duh...
Is it necessary to give declare public class for which contain main() method. or we can can declare any class as a public but that should be saved be Public class name. please clear me me this point.


No and Yes.

1. Until and unless you want the class to be referred, the class does NOT need to be declared as public. However if you want the class to be executed by JVM, the main should have the exact signature (public static void main( String[] args)).

2. A .java class can have many classes inside. However one of them should be saved as a public class and that should match with the name of the .java class.
 
Prakash Rai
Ranch Hand
Posts: 107
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavan Muthu wrote:Very good questions


Somprakash Rai wrote:
Q1. Why we can not assign final variable inside any method other than constructor?



1. Because, that variable is final. A variable declared as a final should be initialized within the initialization period of its associated object (instance). An initialization period of an object (instantiation process) is said to be completed in Constructor. Thats why you use new to instantiate. So till that time you are allowed to postpone the initialization of the variable.

Generally we initialize the value of a final variable during the declaration itself. Otherwise, we go with constructors. If not through any of these, the compiler would complain saying that you are violating the contract of the final!
Thank you Muthu. really it is very nice reply. Thanks.
Somprakash Rai wrote:
Q2. Is it always necessary to assign final variable? Suppose if i have't initialize final variable then why it is not taking default value.?
When I removed this this multiple line comments then It was giving error.


Yes of course. You have to assign a value. If you think you can assign the value later, the variable will no longer be of final. The answer is interlinked with the previous answer.

Hope this helps!
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somprakash Rai wrote:
Thank you Muthu. really it is very nice reply. Thanks.


You are welcome Somprakash Rai

While replying you have to ensure you don't type your answer within or between "quote.." and "/quote" tags (Add a pair of square brackets for each quote term].

If so, your answer gets merged within the quoted text.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavan Muthu wrote: . . . A .java class . . . .java class.
I presume by ".java class" you mean ".java file".
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I presume by ".java class" you mean ".java file".


Yes, exactly!

I just told that way so that the OP will get an analogy. However I meant the .java file only.

Thanks CR for mentioning it!
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somprakash Rai,
Your post was moved to a new topic.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!