About Question enthuware.ocajp.i.v7.2.928 :

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

Moderator: admin

Post Reply
deepa.patre
Posts: 15
Joined: Thu Dec 13, 2012 9:44 am
Contact:

About Question enthuware.ocajp.i.v7.2.928 :

Post by deepa.patre »

I have a doubt in this question.

Why can't i use int a and static int a in one class?
I know it shows error when u type... but i understand why... because they both are different one is a static variable and an other is a instance variable.

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

Re: About Question enthuware.ocajp.i.v7.2.928 :

Post by admin »

There is nothing much to it except that that is how the language designers designed the language. Basically, it would be too confusing to have two fields with the same name.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

elit3x
Posts: 7
Joined: Tue Oct 04, 2016 6:11 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.928 :

Post by elit3x »

I do not understand why //2 and //3 could occur in the same class...

static int a; // (2)
int f( ) { return a; } // (3)

Since 3 does not declare its own `a` variable, It would be accessing the static `a` declared in (2).
I thought static methods could only access static variables and vice versa?

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

Re: About Question enthuware.ocajp.i.v7.2.928 :

Post by admin »

It is true that static methods can access only static members of the class. But instance methods can access static as well as instance members and f() is an instance method.


HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Dellbell
Posts: 1
Joined: Mon May 28, 2018 5:41 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.928 :

Post by Dellbell »

Hi,

Im wondering whether or not there should be a 3rd option. How can you have 2 methods with the same name? Line 3 and line 4 are two methods with the same name except one is static and the other is not. When I write the same code in intellij, I get compiler error saying method f already defined.

Thanks

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

Re: About Question enthuware.ocajp.i.v7.2.928 :

Post by admin »

Yes, it can have that option also. But a question does not necessarily have to have all the possibilities.
If you like our products and services, please help us by posting your review here.

Denyo1986
Posts: 38
Joined: Thu Jan 07, 2021 2:47 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.928 :

Post by Denyo1986 »

Given the following set of member declarations, which of the following is true?

int a; // (1)
static int a; // (2)
int f( ) { return a; } // (3)
static int f( ) { return a; } // (4)


Consider the following example:


class Test{

static int a;

public int testMethod(){
int a = blabla;
}


Compiles without errors, so these member declarations can both occur within the same class definition (assuming that a class's method definitions are part of the class's definiton, which I assume is true).

Curious to hear your comment.

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

Re: About Question enthuware.ocajp.i.v7.2.928 :

Post by admin »

The problem statement says, "member declarations". In your example, int a = blabla is not a member declaration.
If you like our products and services, please help us by posting your review here.

iulian
Posts: 4
Joined: Wed Nov 22, 2023 1:59 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.928 :

Post by iulian »

Hi,
the explanation says: "declaration (4) defines a static method that tries to access a variable named 'a' which is not locally declared. Since the method is static, this access will only be valid if variable 'a' is declared static within the class. Therefore declarations (1) and (4) cannot occur in the same definition."
If I write:
public class TestStatic {
int x;//not locally declared;

public static void main(String[] args) {
int x = 5;
System.out.println(x);
}
}

This compiles fine, output 5. Also Dr. Sean Kennedy says: "int x outside of main in your class and then in main x is assigned 5. It's a compiler error".

Why then Declarations (1) and (4) cannot occur in the same class definition ? Why does the above code not contradict the statement "Declarations (1) and (4) ... "?

Thank you,

Iulian

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

Re: About Question enthuware.ocajp.i.v7.2.928 :

Post by admin »

Because in (4), a local variable is not being defined. It is just trying to access "a" without defining it and so the compiler will assume that it is trying to access a member variable and compiler will find that a member variable "a" exists but that variable is not static. It is, therefore, not accessible inside (4), which is a static method.

In your code, you are defining a new local variable x. A Local variable always shadows a member variable with the same name so the print statement is accessing that local variable and not the member variable x.
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 37 guests