About Question enthuware.ocajp.i.v8.2.1452 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
nagarjuna19
Posts: 5
Joined: Sat Mar 17, 2018 6:21 pm
Contact:

About Question enthuware.ocajp.i.v8.2.1452 :

Post by nagarjuna19 »

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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1452 :

Post by admin »

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.

nagarjuna19
Posts: 5
Joined: Sat Mar 17, 2018 6:21 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1452 :

Post by nagarjuna19 »

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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1452 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

ash4413
Posts: 4
Joined: Thu Jul 26, 2018 7:48 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1452 :

Post by ash4413 »

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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1452 :

Post by admin »

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.

qazwsx922
Posts: 7
Joined: Sat Sep 04, 2021 8:17 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1452 :

Post by qazwsx922 »

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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1452 :

Post by admin »

>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).
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 65 guests