Problem with hello world tutorial in dcoder [duplicate] - java

This question already has answers here:
Error: class X is public should be declared in a file named X.java
(19 answers)
Closed 4 years ago.
New to coding. Searched following query to no avail.
I'm using Dcoder IDE and trying the Hello World tutorial.
The following error message occurs when I try to output my code.
source_file.java:1: error: class HelloWorld1 is public,
should be declared in a file named HelloWorld1.java
public class HelloWorld1
^
1 error
My code is as follows
public class HelloWorld1
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
The file is saved as HelloWorld1.java
How do I resolve this?
Thanks

Your file is saved as source_file.java. In order for your code to compile, it must be named HelloWorld1.java.
To improve the readability of your code it is often a good practice to indent, like this:
public class HelloWorld1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

Are you sure that the file is saved as HelloWorld1.java? Because in the output you posted it reads it as "source_file.java".

Related

Does ".class" work for instances of a class in java [duplicate]

This question already has answers here:
What is the difference between a.getClass() and A.class in Java?
(7 answers)
Closed 5 years ago.
I have tried to execute the given code given below:
public class XXX
{
public static void main(String[] args)
{System.out.println(new XXX().class);
}
}
But the code shows an error during compilation:
/XXX.java:4: error: <identifier> expected
{System.out.println(new XXX().class);
^
/XXX.java:4: error: ';' expected
{System.out.println(new XXX().class);
^
2 errors
But when I compile the following code:
public class XXX
{
public static void main(String[] args)
{System.out.println(XXX.class);
}
}
it works fine,I mean it prints the output as given below
class XXX
Does this mean that the ".class" operation( I don't know what to call it) in java is only meant for a class and not its instances ?
For instances you have to use the method getClass()
public class XXX
{
public static void main(String[] args)
{System.out.println(new XXX().getClass());
}
}

java static method why is needed [duplicate]

This question already has answers here:
"Non-static method cannot be referenced from a static context" error
(4 answers)
Closed 6 years ago.
I'm using eclipse as IDE for java.
I wrote the following code, but I have one error on loadStrade(). Eclipse suggested me to change loadStrade from public void to public static, and I don't understand why?
I've looked for similar problem and I've found some problems like mine, but I still not understand why I have to change method to static. Uffa!
In the code, routesNet is a graph (jgraphT), and loadStrade() is used to populate vertex and edge.
Can I have help. Thanks, Fabrizio
public class GestioneStrade {
private Stradario routesNet;
public static void main(String[] args) {
/*
* Instantiate Stradario and fill it with routes and cross
*
*/
GestioneStrade m = new GestioneStrade(); //istance of gestionestrade ok?
// now I set new routesNet
m.setRoutesNet(new Stradario());
loadStrade(m.getRoutesNet()); // why loadStrade must be static :-(
}
public Stradario getRoutesNet() {
return routesNet;
}
public void setRoutesNet(Stradario routesNet) {
this.routesNet = routesNet;
}
public void loadStrade(Stradario str) {
// some code to fill routesNet
}
In "main" you should replace
loadStrade(m.getRoutesNet());
to
m.loadStrade(m.getRoutesNet());
And leave loadStrade as non-static.

Class name same as filename in java? [duplicate]

This question already has answers here:
Why are filenames in Java the same as the public class name? [closed]
(7 answers)
Closed 8 years ago.
in java, filename should be same as that of main class. It is the way of telling compiler that this is the entry point for you. but why this thing works:
class xyz{
public static void main(String[] args){
System.out.println("a");
}
}
even when saved with different filename.
And why this thing does not when saved with diffrent filename:
public class xyz{
public static void main(String[] args){
System.out.println("a");
}
}
public classes have to be in a file with the correct filename. Non-public classes can be in any file you want. Even multiple classes in the same file if it is convenient.
Note that:
class xyz
Is not a public class so it cannot be acessed from outside of the file. Therefor it does not need to have the same name. But in this case:
public class xyz
You do have a public classe, that it gonna be acessed from outside of the file, so it does need to have the same name.
Conclusion: public classes need to have the file name exatly the same as the class.

How to write a Hello World application in Java? [duplicate]

This question already has answers here:
Why main() method is needed in java main class
(6 answers)
Closed 5 years ago.
I have been trying to make the Hello World Java application. But when I try to run the program it says that my selection has no main type. Here is my source code.
public class HelloWorldClass {
System.out.println("Hello world!");
}
Because it has no main() method where the code would take the starting point. Use this
public class HelloWorldClass {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
Please note there are some of the things you should always take a note of while programming. Which is also known as the syntax of the language.
Java requires you to have the class written as the FileName, HelloWorldClass is the name of the file of yours.
Then, any data type of it. In my case it is void which means it won't return anything to you in the end.
Also, you should write String[] args which is the Parameter to the method. I was last night trying to understand why I should write these? There is a method, which runs without these, but Java recommends you to add the parameters.
When running Java you need a main method so the compiler knows what to run:
public class HelloWorldClass {
public static void main(String[] args)
{
System.out.println("Hello world!");
}
}

Exception in thread "main" java.lang.NoSuchMethodError: main [duplicate]

This question already has answers here:
"Error: Main method not found in class MyClass, please define the main method as..."
(10 answers)
Closed 9 years ago.
when i try to compile this:
public class Risk
{
}
class territory
{
public static void main (String[]arg)
{
System.out.println ("hi") ;
}
}
I get this error message:
Exception in thread "main" java.lang.NoSuchMethodError: main
whats going wrong here?
The class containing the main() function must be public, and you may only define one public class per file. You'll want to have two separate files Risk.java and Territory.java.
Risk.java:
public class Risk {
}
Territory.java:
public class Territory
{
public static void main (String[]arg)
{
System.out.println ("hi") ;
}
}
EDIT: It turns out this isn't true - I was able to run your initial code with the following command line:
java territory
But my earlier comments point to the best practice for a real app, such as a Risk game.
What class are you trying to run? If you're using the class territory, that will work. Risk has no main method, though.
Can you figure out why this example causes the same issue?
public class Simple {
public void main(String args[]) {
System.out.println("Inside function");
}
}
Answer: because main() should be public static void!
Could it just be a spacing issue? Your original post shows no space between the ']' and 'arg'.
Try this:
public static void main (String[] arg)
or, if that still doesn't work:
public static void main (String arg[])
What the answer wound up being was that the class i run must contain main or else it won't work. i'm posting this because, though other answers give roughly the same information, they don't make it explicit.

Categories

Resources