Plan your text ad campaign.
World class Hard Drive Recovery and renowned raid recovery services
WestNIC provides reliable web hosting services
Free software downloads and drivers download resources
This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQs and more.
equals and ==?
A: A Java class is a definition or model of an object type. A class has a specific set of fields, methods and constructors with distinct argument types. Any object that fulfills a class definition directly or by inheritance has a set of properties and behaviour that is common to all instances of the class. In this sense, a class is like a set of things that are alike.
In Java concrete classes also provide a code implementation that can be instantiated to create an object reference. An instance of a class directly fulfills the its own definition, it also fulfills any superclass definitions too.
The Java Virtual Machine creates static references to classes when it runs a Java program. Classloaders make the public static fields and methods of classes available to the runtime system whether any instance exists or not. When a constructor is called, the class returns an instance of the object it represents.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: In Java the Object class is the ultimate superclass of every other object type. All objects are extended from the Object class, either directly or by inheritance through any number of parent classes. If a class does not explicitly extend any named class, it implicitly extends the Object class. An object with a small o is the word used to describe an instance of a Java class.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: It is possible to use the same variable names for method local variables in two separate methods, and in separate statement blocks, enclosed by curly braces, { and }. Methods and statement blocks have their own variable scope, so the Java compiler and interpreter can maintain a distinction between the variables they contain. Generally it is preferable for variables that represent different properties to have different names to avoid confusion, but it is likely that several methods may use an index named i in a for loop for example.
public class MethodLocalVariables {
void testOne() {
int test;
}
void testTwo() {
int test;
}
}
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: It is technically possible to use the same variable name in separate statement blocks because the variables are localised; the compiler can deduce from their context that they are different things, see the working example below. However, it is not advisable to use the same variable name in multiple places, especially within the same method, because it is confusing and can lead to bugs.
public class RepeatLocalVariables {
public final void doSomething(final String thing) {
if ("thing one".equals(thing)) {
int a;
// Other statements
}
if ("thing two".equals(thing)) {
int a;
// Other statements
}
}
}
If both variables in this example are actually used for the same purpose, a single variable declaration should be made in the main body of the method. If the variables are for different purposes, it would be better to name them differently.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: The syntax for methods with no arguments is to follow the method name with open and close brackets with nothing in between. This syntax applies to static and instance methods, and those with void, primitive or object return types.
public final void noArguments() {
// Method statements
}
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: In most cases it is not relevant or necessary for a Java method to know the object that called it. If your code needs to know the origin of a method call it is likely the method is located in the wrong host class, or your overall program structure does not follow good object oriented principles. Consider whether you can move the method to a different host class or refactor to place class-specific code in the relevant classes.
If you find there really is good reason to know the origin of method calls, add an Object argument to the method and use the getClass() method to test.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: Host classes call methods on inner classes in exactly the same way as they would on a separate class defined in its own compilation unit. To call an inner class' instance method it is necessary to instantiate the class first, as below.
More details available to premium content service subscribers:
How does an object call an inner class method?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: A Java method header is the whole declaration statement for a method before its curly braces. The header includes the method's visibility modifier, return type, arguments and exceptions, as below.
public final String getDetails(final File file,
final String key) throws IOException
A Java method signature is the method name and parameters only. The order of the parameters is significant because they may distinguish overloaded methods by the same name.
getDetails(File, String)
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: One technique you can use to track the number of instances of an object is to create a public newInstance() method that all clients must use and declare a default constructor that is private. Only the newInstance() method can be used to create new instances, so it can also count them as they are issued, as in the example below.
More details available to premium content service subscribers:
How can I count the number of instances of an object?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: Yes, sometimes it is sensible to use an object to carry other object references instead of an array. For instance, you could issue an object as the return value of a method that must return multiple object references.
More details available to premium content service subscribers:
Can objects be used in place of arrays?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
equals and ==?
A: The Java == operator is used to compare primitive values such as int, long and boolean for equality; whether the variables, values or expression on either side of the operator equate to the same value.
More details available to premium content service subscribers:
What's the difference between equals and ==?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: Java object constructors are invoked by putting the new operator before the class name and enclosing any constructor argument references in parentheses after it. A fundamental case is the Object class, which is invoked as follows.
More details available to premium content service subscribers:
How do I invoke a constructor?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: When you have a number of constructors in a class you can call them using this() in a similar way to the superclass constructor super(). For instance, if you have a "good citizen" constructor that takes a String and a boolean, and a shorthand version that only takes a String, you may pass a default value to the two argument constructor, like so:
More details available to premium content service subscribers:
How can I call a constructor from a constructor?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
| Front-end FAQs | Back-end FAQs | Learning Java |
|---|---|---|
About us: site help, text ads, sponsored links and premium content.