I was writing a code in which I print a statement "Hello World" but an error occur named cannot find symbol. I tried hard to remove this error but failed.
public class Input{
System.out.println("Hello World");}
This is the statement and the error
Please anyone can help me in resolving this error and tell me why this error occur so I will not repeat this mistake in future.
Use this :
public class Input {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
You can't print in the scope of a class. You need to inform more about the difference between a function and a class.
A "class" is sort of like a noun. It's the person, place or thing.
public class myThing {
//This is a comment
//put stuff that describes the thing in the curly braces
}
In order to make the thing do something, you must put it in a method
public void myMethod(){
//code in here gets run when myMethod runs
}
Methods must be in a class though. A method can usually be though of as "something a thing can do"
public class myThing {
public void myThingCanDoThis(){
//does this stuff only when called from somewhere else
}
}
The main method is a special type of method that always looks like this:
public static void main(String[] args) {
//This special method gets run automatically when your program runs
}
Your hello world should define a thing. It should have the method, or "verb", of "main".
public class myThing {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
For now just assume that all your java code must go inside of the main method. Learn the basics and then when you learn more start to pay attention to things like "public", "static", "class" , "void" etc. It will all make sense in time
Related
how to run "print()" method when class a created as object? I want to run "print()" method right after the line "a obj = new a();"
I mean, just call the class, not the method. That it will operate immediately after the calling
class a {
public static void print() {
System.out.println("Hey!");
}
}
public class MyClass {
public static void main(String[] args) {
a obj = new a(); // I want to run print() method right after it
}
}
I guess if this is some sort of weird java puzzler (why the heck wouldn't you just put print() in the constructor?), you can put it in an initializer:
class a {
{ print(); }
public static void print() {
System.out.println("Hey!");
}
}
will get the job done. But this is a very silly idea - at the class file level it's all the same thing, whether you do this or put the print() statement in a constructor. We're just nitpicking on language features at this point.
Perhaps take a step back. You had some unknown problem. You thought: I know! I'll just somehow make construction of objects of this class cause the print method to run, but without putting a call to print in the constructor! Oh, but, how do I do that - better ask SO.
That was the wrong thought. So state the unknown problem instead of a question about a bad 'solution'.
NB: You can also make static initializers, e.g:
class a {
static { print(); }
public static void print() {
System.out.println("Hey!");
}
}
And now print() will in fact run before that constructor. It'll also run only once, ever, that's the point of static initializers: They run the moment you so much as look funny at that a class (do anything with it at all), and after they've run they are never run again.
Your question is quite unclear, so I'm just taking wild stabs here.
Bad english alert
Whenever I try to use System.out.println on another class besides main, every single IDE installed in my PC returns the error on the title.
I'm writing a really simple code.
On IntelliJ, I had already tried to use "Invalidated caches" but didn't work as well.
Works here:
package javaapplication3;
public class JavaApplication3 {
public static void main(String[] args) {
System.out.println("Hi"); /* <-- This works on main, but doesn't
work in any other class opened in
another tab*/
}
But not in this other tab:
package javaapplication3;
public class NewClass {
System.out.println("Hi");
}
UPDATE
Here some images to specify the problem: Work here, but not here.
All code has to be in methods.
The command System.out.println("") will work only in a method.
Placing it under a class but not a method will result in the compiler throwing an error.
Eg:
public class test { // class
public static void main(String args[]) {
// inside main method
System.out.println("Hello, World!"); // correct
}
}
will work perfectly fine.
But, if you place the command under just a class, it's going to result in an error.[Needs to be in a certain method]
Eg:
public class test{
// inside a class, but no method
System.out.println("Hello, World!"); //incorrect
}
Also, you need to make sure your class and function are not reserved keywords.
And from what you've specified above, main in not a class but it's the main method of the class.
I compiled this code and then, TestInner$1.class emerged.
I know ~~~$1.class indicates that file has "anonymous class."
But I don't understand the reason why this class file made. I want to know the reason.
Here is the code.
public class TestInner {
private static class Inner { }
public static void main(String[] args){
new Inner();
}
}
I tried another version removed "private" identifier, like the following.
public class TestInner {
static class Inner { }
public static void main(String[] args){
new Inner();
}
}
I'd imagined that this code also would make TestInner$1.class file.
However it didn't create the file.
In addition, the following code, added Constructor, also didn't make TestInner$1.class.
public class TestInner {
private static class Inner {
Inner(){ }
}
public static void main(String[] args){
new Inner();
}
}
I have no idea, so can anyone help me?
EDIT:
I found the same question and it solved. Thank you for your helping.
Why is an anonymous inner class containing nothing generated from this code?
None of your examples have anonymous inner classes. None of them will produce a file named TestInner$1.class. All of them will produce a file named TestInner$Inner.class.
The following example shows an anonymous inner class and will produce TestInner$1.class:
public class TestInner {
public static void main(String[] args){
new Object() {
#Override public String toString () {
return "ninja";
}
};
}
}
I'm not sure where your TestInner$1.class came from but I'm guessing it's left over from previous experiments you were doing.
Update 1: I can confirm that without using Eclipse I get TestInner$1.class (in addition to TestInner$Inner.class -- 3 files are produced) for the first example but not for the last two, just like you are seeing. Will update when I find out why. When compiled via Eclipse, TestInner$1.class is never produced.
Update 2: OP found solution in Why is an anonymous inner class containing nothing generated from this code?.
Eclipse allows me to run this by simply pressing a proceed button in a "Error in work-space" dialog:
class MainClass
{
public static void foo()
{
}
public static void foo()
{
}
public static void main (String[] args)
{
}
}
Does this mean that function redefinition's like these are legal?
Eclipse allows you to run code which doesn't compile. It replaces such code with throw new Error("whatever the compilation error was"); I consider this a very BAD (Broken As Designed) feature but others love it.
In your case this works because you don't use the function anyway.
no it is illegal, it must differ in method signature
Two methods are prohibited from having the same signature this won't compile.
check again, you should get following error
Duplicate method foo() in type MainClass
For example I have this code:
public class A
{
private void my_method(){
//do something
}
}
So how can I call that method for code below to use it?
I saw in one example it was done like this:
public class A
{
public A {
my_method();
}
//some other code
private void my_method(){
//do something
}
}
But trying this gives me this error:
"Syntax error on token "public", class expected after this token"
And of course using advise in error, gives this error:
"The nested type A cannot hide an enclosing type"
So it seems that code I saw is bad or somehow I'm doing something wrong. Anyone could explain how to do it properly in Java?
Your constructor is wrong (you forgot the brackets).
It has to be
public A() {
}
constructor is missing (). use
public A()
{
}
You are getting this error because you have not wrote the constructor correctly.
It should be:
public A() {
my_method();
}
Just to expand on Jeroen's answer as it seems you are quite new to Java:
Your private method can be called from inside another method in your class. E.g.
public class A
{
public void anotherMethod() {
my_method();
}
private void my_method(){
//do something
}
}
The code you provided was called within the constructor of the class. This is a special method which is called when an object of type A is constructed e.g. new A();. You can tell it's a constructor because it has no return type specified:
public A() {
}
rather than a normal method:
public void a() {
}
Something to note there is that in Java it is convention (but not strictly required) to name normal methods with a lowercase first letter and classes/objects/constructors with an upper case first letter.
So your mistake was that in your constructor you had not put () after the method name (A in this case).