Question

Explain what happens if you: Attempting to refer to an instance field or instance method in...

Explain what happens if you:

  • Attempting to refer to an instance field or instance method in a static method.
  • In a method that accepts an object as an argument, writing code that accidentally modifies the object.
  • Allowing a null reference to be used.

       What is the course of actions that you should take to avoid these situations.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

WHEN ACCESSING THE INSTANCE FIELD FROM STATIC METHOD

The static method can only accept the static variables.Therefore,
the static method throws an error that the non-static or instance variable
cannot be referenced from the static method.


COURSE OF ACTION TO BE TAKEN WHEN ACCESSING
A NULL REFERENCE OBJECT

In a method that accepts an object as an argument ,then there is a probability
of modify the object .So, we need to pre-check using if case that do modify
the object only if the object is not null.So that that null reference is not thrown when user try to modify it.

Add a comment
Know the answer?
Add Answer to:
Explain what happens if you: Attempting to refer to an instance field or instance method in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Y. Daniel Liang’s 8 Class Design Guidelines were posted on the File Manager and handed out...

    Y. Daniel Liang’s 8 Class Design Guidelines were posted on the File Manager and handed out in class. Please choose 5 guidelines and discuss them in depth. For each guideline, use 1 page or more for your discussion. You can use the code provided in class to demonstrate your points. The code should not be more than one-third of your writing. 1. Cohesion • [✓] A class should describe a single entity, and all the class operations should logically fit...

  • Can you please give me an answer asap. Thank you. Can you please just explain what...

    Can you please give me an answer asap. Thank you. Can you please just explain what the method does and provide a recurrence relation for the method. And State the method's runtime in big-Oh notation of following. Question no 2) //This is an instance method for MyLinkedList from project 1. //For writing the recurrence relation, n is size - index of node. 1/Precondition: node is an element of the list public void problem2 (MyListNode<E> node) { if(node.getNext() != null){ problem2...

  • a derived class from Organism. You must complete the constructors, and the method definitions that were...

    a derived class from Organism. You must complete the constructors, and the method definitions that were abstract methods in Organism. /** Organism.java * Definition for the Organism base class. * Each organism has a reference back to the World object so it can move * itself about in the world. */ public abstract class Organism {    protected int x, y;       // Position in the world    protected boolean moved;   // boolean to indicate if moved this turn   ...

  • QUESTION 13 A constructor is the same as any other method definition EXCEPT for what? A...

    QUESTION 13 A constructor is the same as any other method definition EXCEPT for what? A constructor includes an optional argument list. A constructor includes an optional access modifier, usually public. A constructor includes a return type. A constructor places arguments within parentheses (). 2 points    QUESTION 14 Using the asterisk wildcard (e.g. import com.packageName.*) will include all of the classes within packageName, even if they aren’t used. True False 2 points    QUESTION 15 The Random class gives...

  • JAVA :The following are descriptions of classes that you will create. Think of these as service...

    JAVA :The following are descriptions of classes that you will create. Think of these as service providers. They provide a service to who ever want to use them. You will also write a TestClass with a main() method that fully exercises the classes that you create. To exercise a class, make some instances of the class, and call the methods of the class using these objects. Paste in the output from the test program that demonstrates your classes’ functionality. Testing...

  • 1. What is an abstract method and why are they useful? Illustrate your answer using an...

    1. What is an abstract method and why are they useful? Illustrate your answer using an example of where you might use an abstract method. 2. What is the difference between a static variable and a non-static variable? Given the example of a class representing Dogs, give an example of a variable that may be static and another that may non-static. 3. In Java, when you modify a String as shown in the code below, Java makes a new String...

  • This exercise guides you through the process of converting an Area and Perimeter application from a...

    This exercise guides you through the process of converting an Area and Perimeter application from a procedural application to an object-oriented application. Create and use an object 1. Open the project named ch04_ex2_Area and Perimeter that’s stored in the ex_starts folder. Then, review the code for the Main class. 2. Create a class named Rectangle and store it in the murach.rectangle package. 3. In the Rectangle class, add instance variables for length and width. The, code the get and set...

  • JAVA /** * This class stores information about an instructor. */ public class Instructor { private...

    JAVA /** * This class stores information about an instructor. */ public class Instructor { private String lastName, // Last name firstName, // First name officeNumber; // Office number /** * This constructor accepts arguments for the * last name, first name, and office number. */ public Instructor(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } /** * The set method sets each field. */ public void set(String lname, String fname, String...

  • Purpose: To learn how to create and use stand-alone class programs (classes withouta main method)...

    LE 7.2 Purpose: To learn how to create and use stand-alone class programs (classes withouta main method). When you take code out of the application class and put it in a class of its own, you make it reusable Prep Work: Chapter 7 Figure 7.6 shows you how an application class uses the services of a Java class in Figure 7.5. Although the code for LE 7.2 is different than the code in these figures, the concept is the same....

  • This wont take you more than 15 mins. Comple the two method and besure to test...

    This wont take you more than 15 mins. Comple the two method and besure to test with Junit test that provided below. toArray() -- this method returns a newly allocated array containing the elements in the multiset. The array this method returns must only contain the elements in the multiset and not any nulls or other values that are stored in the backing store, but are not in the multiset. fromArray(E[] arr) -- this method updates the multiset so that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT