why this class not compiling? [duplicate] - java

This question already has answers here:
Why can't I do assignment outside a method?
(7 answers)
compilation error: identifier expected
(4 answers)
Closed 5 years ago.
Why is this error coming? Please see the following code.
class Test{
Hello h=new Hello();
}
class Hello{
int a=10;
System.out.println(a); // error identifier expected
}

Create Class With Same Package
public class Hello {
public void print(){
int a = 10;
System.out.println("Number is :" +a);
}
}
Crate Class to Set the Main Method Within the Same Package of Hello Method
public class Main {
public static void main(String args[]){
Hello h1 = new Hello();
h1.print();
}
}

Related

Can anyone remove error from my java program? | Tool: VS Code | error: can't find main(String[]) method in class: myjavacode.Base [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 2 years ago.
package myjavacode;
// using vs code to run program
class Base{
int x;
public int getX() {
return x;
}
public void setX(int x) {
System.out.println("I am in base setting x now");
this.x = x;
}
}
class Derived extends Base{}
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello World");
Derived obj = new Derived();
obj.setX(4);
System.out.println(obj.getX());
}
}
Java main method is the entry point of any java program. so you can not run any class without this entry point, it seems you are trying to run the code from the Base class which do not have this main method.
public static void main(String[] args)

How to access a method from the main function in java [duplicate]

This question already has answers here:
Calling Non-Static Method In Static Method In Java [duplicate]
(14 answers)
Closed 6 years ago.
Here's my code:
public class Wallgame {
public static void main(String[] args) {
new Hello();
}
public void Hello() {
System.out.println("Hello!");
}
}
This does not work, and I don't know why I can't access the Hello method. Thanks!
Hello method should be static and you call it by writing Hello(); in the main method.

Does static variable inherited? [duplicate]

This question already has answers here:
Are static variables inherited
(4 answers)
Closed 4 years ago.
We know that in java static variable are not inherited. But in below code I am not getting any error as I want to initialize the static variable in child class.
class s
{
static int x;
}
class aaa extends s
{
void fun()
{
x=2;
System.out.println(x);
}
public static void main(String args[])
{
aaa w=new aaa();
w.fun();
}
}
static members are most definitely accessible from subclasses, as your example shows. You cannot override them, of course, but you could hide them.

java Error: Could not find or load main class TestBus [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 6 years ago.
public class Sample2 {
int a=11;
int b = 22;
}
class DemoBus extends Sample2 {
int a=25;
int b=26;
void m1(){
System.out.println("demo class m2");
}
}
class TestBus extends DemoBus
{
int d=65;
int e = 78;
void m2(){
System.out.println("sample class m2");
}
void m3(){
System.out.println("sample class m3");
}
void show()
{
int a=45;
int c=90;
TestBus t = new TestBus();
System.out.println(t.a);
System.out.println(super.a);
System.out.println(c);
System.out.println(this.a);
System.out.println(d);
System.out.println(a);
m1();
m2();
m3();
}
public static void main(String ar[])
{
TestBus s = new TestBus();
s.show();
}
}
I tried to execute it but it is not executing. I tried with 'public' kept for TestBus, still it's showing error. I also
tried in cmd with TestBus then the code executed and output is displayed
but in ide its showing error.
You should put main method in the public class in your case Sample2 and it should work! I have tried it in NetBeans and it works.
The problem whith your code is that your .java file is named Sample2 and when your program is executed by JRE, it does not find your main method inside your source files (.java files) even if your method is static.

can anybody explain the output of the following Java code? [duplicate]

This question already has an answer here:
Why does it store or allocate memory for super class variables, in sub class object?
(1 answer)
Closed 7 years ago.
class A{
int i = 1;
A(){play();}
void play(){System.out.print(i);}
}
class B extends A{
int i = 2;
B(){play();}
void play(){System.out.print(i);}
}
public class Test extends B{
public static void main(String args[]){
new Test();
}
}
output:
02
can anybody explain the output of the above Java code?Thx.
Because Test class doesn't constructor and it extends from B class. Therefore, it will call B constructor and output is 2.

Categories