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

Question 1 Analyze the code below. Identify all of the illegal method calls and

ID: 3789600 • Letter: Q

Question

Question 1 Analyze the code below. Identify all of the illegal method calls and assignment statements. In your own words, in a sentence or two, for each illegal call or assignment, explain why it is illegal. You are welcome to compile this file to see what the compiler answer is You may also go and research the compiler errors. You might also want to read up on static, private, and public in Java. Ultimately, however, you must explain in your own words why you are getting that error. Some answers will be the same for different statements file: 1. java public class A1 private static int y; private int x; public void foo() t public static void far public static void faz (A1 a) a. X

Explanation / Answer


/*Compile time errors are explained
in bold letters*/
//A1.java
public class A1 {

   private static int y;
   private int x;
  
   public void foo(){
       x=1;
   }
  
   //Error in method
   public static void far(){
       //Compiler Error:
       //Accessing nonstatic variable x
       //from the static method will throw
       //a compiler time error
       //Cannot make a static reference to the non-static field x
       x=2;
   }
  
   public static void faz(A1 a){
       a.x=3;
   }
  
}


----------------------------------------------------------------------------------------------------

//B1.java
public class B1 {

   //Compile time error
   public static void boo(){      
       //Since A1 is unknown object or variable
       //So cannot be access x fraom A1
       A1.x=4;
   }
   //Compile time error
   public static void bar(A1 a){      
       //Here a is an object of class A1
       //Here the object,a trying to access
       //like static variable.
       //The object,a cannot access the non-static
       //x,variable using the object,a
       //Instance variable, x cannot be accessed
       //directly since x is private variable.
       //It is only accessible using public methods
       //of the class A1
       a.x=5;
   }

   //Compile time error
   public void baz(){
       //Cannot access the x variable
       //since A1 visibility is not known
       //to in this method.
       A1.x=6;
   }
   //Compile time error
   public void biz(A1 a){
       //Cannot access the x variable
       //since A1 visibility is not known
       //to in this method.
       //x is a private variable of class,A1
       //Only public method of A1 class
       //can access private variable,x
       a.x=7;
   }


}

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