Page 1 of 1

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

Posted: Fri Jun 07, 2013 3:07 pm
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.

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

Posted: Fri Jun 07, 2013 7:36 pm
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.

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

Posted: Wed Oct 12, 2016 10:54 am
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?

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

Posted: Wed Oct 12, 2016 9:56 pm
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.

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

Posted: Mon May 28, 2018 5:47 pm
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

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

Posted: Mon May 28, 2018 8:15 pm
by admin
Yes, it can have that option also. But a question does not necessarily have to have all the possibilities.

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

Posted: Tue Jan 12, 2021 10:05 am
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.

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

Posted: Tue Jan 12, 2021 11:38 am
by admin
The problem statement says, "member declarations". In your example, int a = blabla is not a member declaration.

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

Posted: Sun Dec 03, 2023 2:43 am
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

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

Posted: Sun Dec 03, 2023 6:27 am
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.