Page 1 of 1

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

Posted: Mon May 23, 2016 11:31 am
by pink.freak
Hi I started using enthuware recently and I have encountered this type of for loop in the Practice Test:

for(int j=1; j <= 5;)

I have answered that it will not compile. However the answer says otherwise.
If I would correct the for loop it would look something like this:

for(int j=1; j <= 5;;)

I would like to clarify why my answer was incorrect.

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

Posted: Mon May 23, 2016 11:48 am
by admin
Hi,
What happened when you tried to compile both the versions (i.e. the one given in the question and the one you have proposed)?
-Paul.

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

Posted: Tue Jan 31, 2017 8:35 am
by Deleted User 3513
In the while loop, j is referring to the instance variable declared above the method showJ()? This currently has the default value of 0. Which is the same reason why I don't have to initialize the j in the while loop? and then the j in the for loop shadows the instance variable j? So j in the while loop doesn't shadow the instance variable j?

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

Posted: Tue Jan 31, 2017 9:07 am
by admin
Deleted User 3513 wrote:In the while loop, j is referring to the instance variable declared above the method showJ()?
Correct.
This currently has the default value of 0. Which is the same reason why I don't have to initialize the j in the while loop?
Correct.
and then the j in the for loop shadows the instance variable j?
Correct.
So j in the while loop doesn't shadow the instance variable j?
No, there is no new j in while loop's scope (the one inside the for loop is not visible outside the for loop). The only j available in while is the instance variable j.

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

Posted: Sun Sep 23, 2018 3:36 am
by abutalib
Hi,

I have a confusion between variable names scope and reference scope. So, based on my understanding I created the following list.

{
There are two types of scopes for variable in Java: name scope and reference scope.

Variable Name scopes:
- Variable names are not allowed to duplicate within one scope.
- They are: 1. outside methods 2. inside methods

Reference scopes:
- They are allowed to be used in the scope they were declared in.
- They are: 1. class 2. instance 3. method scope 4. loop scope 5. if scope 6. switch scope

Both types of scopes are hierarchical. So variable from child scope can access parent scope variables, but parent cannot access child scope variables.
}

Am I understanding this correctly?

Cheers,

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

Posted: Sun Sep 23, 2018 7:02 am
by admin
Yes, that seems logical but "name scope" and "reference scope" are not real or technical terms so I will not be able to vouch for the correctness of what you have written.
You should go through Section 2.1 of OCAJP Associate Java 8 Programmer Certification Fundamentals by Hanumant Deshmukh. You can download it for free from here for the time being.

Go through this section and let me know if you still have doubts about scopes.

HTH,
Paul.

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

Posted: Sun Sep 23, 2018 10:57 am
by abutalib
Thanks Paul, Logical is what I needed to hear.

I got two additional things from the book:
1. you can have nested unnamed block codes with their own scope.
2. Two declared entities with the same name can share the method scope if they do not cross.
Example:
public static void main(String[] args){
{int i = 1; System.out.println(i);}
int i = 2; System.out.println(i);
}

Java never ceases to amaze me. Dear I ask how far does this rabbit hole go?

Cheers,

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

Posted: Sun Sep 23, 2018 10:40 pm
by admin
abutalib wrote:
Sun Sep 23, 2018 10:57 am
Java never ceases to amaze me. Dear I ask how far does this rabbit hole go?
For the purpose of the exam, you have gone far enough :D