Java Questions 31 - 40  «Prev  Next»

Java Questions 38

  1. What will happen if the programmer adds his own constructor to a program?
    Answer:The compiler will not put in a (no-args) default constructor if you already have one or more constructors in your class.
  2. What does the compiler insert when a user defines their own constructor?
    Answer:
    When the compiler does not insert a default constructor, it still inserts a call to super() in any constructor that does not explicitly have a call to the parent constructor.
  3. What is the purpose of using static[1]?
    Answer:
    You will want to use this when the method's behavior has no dependency on the state (instance variable values) of an object.
  4. What is another application of static variables?
    Answer:
    You want to keep a running count of all instances instantiated from a particular class.
  5. What are the default values that static variables receive?
    Answer:
    Static variables receive the same default values that instance variables receive.
  6. What is one application of a static variable in a program?
    Answer:
    You can use a static variable as a counter in your program.

  7. Why is the main() method declared static?
    Answer:
    The main() method is not running against any particular instance of the class.
  8. Why can't a static method directly invoke a non-static method?
    Answer:
    A static method exists before any instance of the class has been created.
  9. How can you mentally distinguish between static and non-static?
    Answer:
    Static is associated with a class and non-static is associated with an instance.
  10. What is the purpose of making the main method static?
    Answer:
    The purpose of this is so that the JVM does not have to create an instance of your class to start running code.
[1]static: Use the non-access modifier static when the method's behavior has no dependency on the state of an object.