Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The scope rules of modern programming languages are rooted in rules developed by

ID: 3741244 • Letter: T

Question

The scope rules of modern programming languages are rooted in rules developed by the earliest block structured languages like Algol. Adding object orientation to languages adds one more layer of complexity to this issue. Let's consider Java, a language with which everyone should be familiar, and explore its scope rules. One aspect of the scope rules of any language is when data or methods can have the same name. Give examples in Java to illustrate a case where declaring two local variables with the same name in same method is permitted and one where it is prohibited. Are the rules governing redeclaration of local names the same as those governing redeclaring local names that rename class-level names? One consequence of scope rules in most languages is that forward references (referring to names before they are declared) are prohibited. Are such forward references always prohibited within a single Java class? Are forward references of local names always prohibited within a single method? If not, provide an example that demonstrates your claim.

Explanation / Answer

Dear,

Feel free to reach out if you have doubts. Please do rate if the answer was helpful. Thanks ----------------------------------------------------------------------------------------------------- Copyable Code: -----------------------------------------

Program:

examples in Java to illustrate a case where declaring two local variables with the same name in same method is permitted and one where it is prohibited:

/*

* Jave will report error when two different local variables are accessible in some scope (some region of the program).

When you compile the program below, you will get this error:

RulesDriver .java:11: y is already defined in main(java.lang.String[])

int y = 20;

^

RulesDriver .java:17: y is already defined in main(java.lang.String[])

String y = "Hello";

*/

public class RulesDriver {

public static void main(String[] args)

{ // Start of Block 1

int x = 0;

double y = 40; // (1)

if ( x > 15 )

{ // Start of Block 2

int y = 20; // (2)

System.out.println(y);

} // End of Block 2

else

{ // Start of Block 3

String y= "hai"; // (3)

System.out.println(y);

} // End of Block 3

}

}//class ends

forward references always prohibited within a single Java class? Are forward references of local names always prohibited within a single method? If not, provide an example that demonstrates your claim.

In simple way it means referencing (accessing a variable, calling a function).As shown down in the piece code.

public class RulesDriver {

int Z = 30;

int X = Y;

int Y = 50;

public static void main(String[] args)

{  

System.out.println("main");

}

}//class ends

/*

the compiler will read it from top to bottom,

so it will se the first line, which declares a variable 'Z', and assigns it to 3, and that is fine, then it will encounter the second line, which declares a variable 'X', and then tries to assign it to 'Y'.

We will get compilation error at int X=Y. forward references is not possible in this case.

So, the forward reference part would be a reference to something that does not yet exist. Forward in time perhaps

*/

Thank You.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote