About Question enthuware.ocajp.i.v8.2.1452 :
Moderator: admin
-
- Posts: 5
- Joined: Sat Mar 17, 2018 6:21 pm
- Contact:
About Question enthuware.ocajp.i.v8.2.1452 :
Hi,
Can you please tell me how below code is valid?
public class A
{
public static void main(String[] args)
{
System.out.println(new A().main); // What does the method new A().main mean?
}
int main; } // what does this mean?
Can you please tell me how below code is valid?
public class A
{
public static void main(String[] args)
{
System.out.println(new A().main); // What does the method new A().main mean?
}
int main; } // what does this mean?
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1452 :
It is creating an instance of A and then accessing the instance variable named main of that instance.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Sat Mar 17, 2018 6:21 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1452 :
hi Admin,
Thank you for reply. Can you please explain bit more.
What does A().main mean, as A a()=new A() is object creation
What does int main; mean?
Regards,
Nagarjuna
Thank you for reply. Can you please explain bit more.
What does A().main mean, as A a()=new A() is object creation
What does int main; mean?
Regards,
Nagarjuna
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1452 :
1. new A() means you are creating an object of class A. You are not assigning it to any variable. The compiler creates a temporary variable to refer to this object.
2. int main; is the definition of a variable named main of type int. Just like int x; or int i; etc.
I suggest you to go through a good book before attempting mock questions.
2. int main; is the definition of a variable named main of type int. Just like int x; or int i; etc.
I suggest you to go through a good book before attempting mock questions.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Thu Jul 26, 2018 7:48 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1452 :
Hi there,
I tried below code
A.java
import java.io.IOException;
class B {
public static void main() throws IOException{}
}
this shows an error message that
Error: Main method not found in class B, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
In simulator, there is nothing wrong with this code
Please clear my doubt
Thank you
I tried below code
A.java
import java.io.IOException;
class B {
public static void main() throws IOException{}
}
this shows an error message that
Error: Main method not found in class B, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
In simulator, there is nothing wrong with this code
Please clear my doubt
Thank you
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1452 :
The question doesn't ask you to run the code. It only asks you whether the code will compile or not. Not every java class requires that it must be run from the command line. So from that perspective, there is nothing wrong with the code.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Sat Sep 04, 2021 8:17 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1452 :
Hello,
//In file A.java
public class A{
int a;
public void m1(){
private int b = 0;
a = b;
}
}
Access modifiers (public/private/protected) are valid only inside the scope of a class, not of a method.
Does it mean all local variables can't have access modifiers?
//In file A.java
public class A{
int a;
public void m1(){
private int b = 0;
a = b;
}
}
Access modifiers (public/private/protected) are valid only inside the scope of a class, not of a method.
Does it mean all local variables can't have access modifiers?
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1452 :
>Does it mean all local variables can't have access modifiers?
Correct. You cannot apply access modifiers (public, private, protected) to local variables (i.e. variables defined within a method).
Correct. You cannot apply access modifiers (public, private, protected) to local variables (i.e. variables defined within a method).
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 117 guests