Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
A snapshot of the code and error being presented
I've tried making class main() I've tried adding {} neither have worked, please help.
You forgot to add a { after main().
public static void main(String[] args) {
//-------------------------------------^
And after return add ; and }.
Looks like you are confusing between Java and .NET. There's no Console.WriteLine() in Java. You need to change it to:
System.out.println();
The Console.WriteLine() is available only in .NET Framework. You need to replace them with the above. For eg:
Console.WriteLine("john smith");
Should be replaced with:
System.out.println("john smith");
If you really want to execute Console.WriteLine, then you need to use Microsoft Visual Studio and create a new C# Console Application.
See more here: How to: Create a C# Console Application:
(source: programcall.com)
And in the Console Program using .NET, it uses the main class as:
class Program {
And not the one which you use. So definitely yours is a Java Application.
You are missing some brackets.
public class MyFirstProgram {
public static void main (String args[]) {
Console.WriteLine("some text");
}
}
EDIT: You should also add semicolons ; to the end of a function/line
For every method with implementation, you need a pair of curly braces.
public static void main(String[] args){
}
The curly braces will indicate the scope of the codes the follows. Also, are you asking a question on C# or Java?
If you are asking a question on Java and not C#, to print a line, you should be using System.out.print() or System.out.println(). Not Console.WriteLine().
It looks like to me you are writing C# codes in Java with a habit carried over from C++. There is no need to write a return statement at the end of your main method in Java.
The return type for main() in Java is void, that means you can't return a value from main().
Your complete program in Java shall look like this:
public class MyFirstProgram
{
public static void main(String[] args)
{ //<--curly bracket needed
System.out.println("john smith");
System.out.println("programming major");
//more println statements below..
} //<--curly bracket needed
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to understand the Java compiler better, and what is happening behind the scenes during compilation and execution, and so I have a question about how Java methods are compiled.
I know that methods need to be called upon or invoked in order for their enclosing code to be executed, and I just want to figure out how this really works
Considering the simple program:
public class Test {
public static void sayHello() {
System.out.println("Hello World");
}
public static void main(String []args){
sayHello();
}
}
Considering that code in java is executed in the order that it appears, why would the contents of sayHello() not be executed, but instead the program waits until the method is invoked for the contents of the method to be executed? So basically I'm asking how does the compiler deal with compiling methods, and what does it do to prevent methods from the contents of a method being run sequentially without them being called on?
Thank you.
Your assertion, "Considering that code in java is executed in the order that it appears", was refuted in comments. However, there is one way in which you could say there is some truth to it, as long as you interpret your statement in a specific way. Let's look at your class:
public class Test {
Here, you're asking the compiler to create a class, and store it in file Test.class.
public static void sayHello() {
This tells the compiler to define a method called sayHello.
System.out.println("Hello World");
This declares to the compiler that this is the content of the method, and to store this content for execution when the method is called.
}
public static void main(String []args){
sayHello();
This tells the compiler to call the method sayHello whenever the method main is called and its body starts getting executed. The method main is called when you run the application with the java command line.
}
}
You see, in a way the contents are "executed" (for some unusual definition of executed) in the order they appear. But that doesn't mean they are run at that time.
The main() function will almost always be executed first.
It is the function that actually contains the code that is run.
Normally you put main() at the top and other functions below it.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
After almost 1 year working with Java,I still can not explain properly to others or myself why we need this, and how it works.
Obviously, I know what each of keywords does independantly, but I am not sure about the whole thing.
Can someone please describe it in very simple language?
Public : is an Access Modifier, which defines who can access this Method. Public means that this Method will be accessible by any Class(If other Classes are able to access this Class.).
Static : is a keyword which identifies the class related thing. This means the given Method or variable is not instance related but Class related. It can be accessed without creating the instance of a Class.
Void : is used to define the Return Type of the Method. It defines what the method can return. Void means the Method will not return any value.
main: is the name of the Method. This Method name is searched by JVM as a starting point for an application with a particular signature only.
String args[] : is the parameter to the main Method.
The other answers are correct, but I'll try to state it in English.
When you create a program, java needs to know where to start the program, so they use this as an entry point. Since it's static method, there doesn't need to be an instantiation of the class. It can simply call that method and know that that's where the program starts.
I knew these already thank you for you response and downvotes.
public
It means that you can call this method from outside of the class you are currently in. This is necessary because this method is being called by the Java runtime system which is not located in you current class.
static
When the JVM makes call to the main method there is no object existing for the class being called therefore it has to have static method to allow invocation from class.
void
Java is platform independent language and if it will return some value then the value may mean different things to different platforms. Also there are other ways to exit the program on a multithreaded system. Detailed explaination.
main
It's just the name of method. This name is fixed and as it's called by the JVM as entry point for an application.
String args[]
These are the arguments of type String that your Java application accepts when you run it.
But why there is no simplar method to call main like in other languages, and why we need to get argument and store as String while we sometimes only call methods in the main.
When you start a Java program the JVM needs to know where it should start the execution of the whole thing. This is not obvious because there are thousands of Java classes available on the classpath.
That's why you must give the name of one particular Java class in your command line. For example
java com.stackoverflow.example.Main bla blah
The JVM then just loads this class, looks for its static void main(String[] args) method, and calls it with the remaining parameters taken from the command line
main(new String[]{"bla", "blah"});
The main() method is defined by the java launcher:
The java command starts a Java application. It does this by starting the Java Runtime Environment (JRE), loading the specified class, and calling that class's main() method. The method must be declared public and static, it must not return any value, and it must accept a String array as a parameter. The method declaration has the following form:
public static void main(String[] args)
That explain why you need it: It's required to start running a Java program.
And how it works: It's called by the launcher at startup.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I just started to learn Java and I want to use Intellij however I'm not able to run any project(even a simple Hello World). I always get this Exception.
Please follow proper syntax for the main method . .
public static void main(String[] args) {
//Your code
}
You have used method name as Main change it to main it will work.
You have a typo in the method name. It must be 'main' always because Java is case sensitive and JVM always looks for a 'main' method to run.
Try this:
public static void main(String[] args) {...}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've been doing a large amount of studying lately, and I was working on C++, and the topic of the hidden pointer (this).
It is one of the most complex topics I have seen so far. I remember this from Java, and I don't remember anything about it being hidden in Java. I do remember that you have to explicitly use it in Java, but apparently it's automatic in C++. Can anyone confirm this?
Hidden? It's not hidden. What does that even mean?
I do remember that you have to explicitly use it in Java
Only in some circumstances. A variable called name could be known inside the class and also be the name of a parameter in a method. Example:
class Test {
String name;
public void test(String name) {
name = name; // What happens?
}
}
Both times name is mentioned it refers to the parameter. The class field is unchanged. You have to tell the compiler that you want this.name if you want the class field.
In other circumstances, when there are no collision in names, the this. part is implicit. Example:
class Test {
String tutorName;
public void test(String name) {
tutorName = name; // What happens?
}
}
The class field is changed even though you didn't use the this keyword.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Getting the class name from a static method in Java
When you are inside of a static method, is there a way to get the class name (a string containing the name) without typing the class name itself?
For example, typing MyClass.class.getName() is no more useful than just "Myclass".
You can use an anonymous inner class:
class Test {
public static void main(String[] args) {
String className = new Object(){}.getClass().getEnclosingClass().getName();
System.out.println(className);
}
}
You can do it by creating (not throwing) a new Exception and inspecting its stack trace. Your class will be the zeroth element as it is the origin of the Exception. It kinda feels wrong, but it will work.
System.out.println( new Exception().getStackTrace()[0].getClassName() );
You can do the same with the Thread class. This seems cleaner to me, but the line is slightly longer. Your class is now the first element in the stacktrace rather than the zeroth. Thread.getStackTrace() is the zeroth.
System.out.println( Thread.currentThread().getStackTrace()[1].getClassName() );
For example, typing MyClass.class.getName() is no more useful than just "Myclass".
On the contrary, if you rename MyClass using your IDE's refactor function it will replace MyClass.class.getName() with RenamedClass.class.getName(). If you put a string in there you'll have to do it manually.
If it is just a matter of saving some typing, then don't think about it. Using MyClass.class allows all refactoring tools to recognize and work properly with your code.
When you choose to program in Java, you WILL type a lot. Use an IDE which will help you so you don't have to type quite as much :)