The simple answer is : no, you can't.
The explanation why is simple. You don't return any value whatsoever from an object constructor. The object has already been created - in the constructor you're just initializing the object's state. So there isn't any way to return a null value.
However, if you want to throw a spanner in the works, and stop someone using your object (which is usually the intent of returning a null value, or to indicate an error), why not throw a NullPointerException ?
public MyClass()
{
// Something might go wrong, so throw a null pointer exception
throw new NullPointerException() ;
}
www.referjava.com
Saturday, February 17, 2007
How I can find the class of an object?
That's easy. Suppose you've got some objects stored in a collection, like a Vector or a List of some sort. You might want to check to see if an individual object belongs to a particular class. The instanceof operator is used in this case.
For example:-
if (obj instanceof MyClass)
{
MyClass.doSomething();
}
else
{
// handle object differently .......
}
www.referjava.com
For example:-
if (obj instanceof MyClass)
{
MyClass.doSomething();
}
else
{
// handle object differently .......
}
www.referjava.com
How can I append an existing file in Java?
Appending data to a file is a fairly common task. You'll be surprised to know that there was no support for file appending in JDK1.02, and developers supporting that platform are forced to re-write the entire file to a temporary file, and then overwrite the original. As most users support either JDK1.1 or the Java 2 platform, you'll probably be able to use the following FileOutputStream constructor to append data:
public FileOutputStream(String name,
boolean append)
throws FileNotFoundException
Parameters:
name - the system-dependent file name
append - if true, then bytes will be written to the end of the file rather than the beginning
For example, to append the file 'autoexec.bat' to add a new path statement on a Wintel machine, you could do the following:
FileOutputStream appendedFile = new FileOutputStream
("c:\\autoexec.bat", true);
www.referjava.com
public FileOutputStream(String name,
boolean append)
throws FileNotFoundException
Parameters:
name - the system-dependent file name
append - if true, then bytes will be written to the end of the file rather than the beginning
For example, to append the file 'autoexec.bat' to add a new path statement on a Wintel machine, you could do the following:
FileOutputStream appendedFile = new FileOutputStream
("c:\\autoexec.bat", true);
www.referjava.com
How do I kill a thread? The stop() method is deprecated in JDK1.2, so how do I shut it down?
The API documentation for JDK1.2 discusses an alternate mechanism for stopping threads, by having them continually poll a boolean flag to see if they should terminate of their own accord. This is an option, but if you have threads that become deadlocked or stall waiting for I/O, sometimes you'll have to kill them the hard way.
Why shouldn't you normally use the Thread.stop() method? Well, it is deprecated as of JDK1.2 because it can potentially leave the system in an unsafe state. If a thread
had a lock on an object (within a synchronized block), it might not release the object lock, causing problems at a later time. Note that this problem can affect older JVM implementations as well, JDK1.02 and JDK1.1 are not immune.
So while you shouldn't use Thread.stop() unless absolutely necessary, it is an acceptable way to kill a thread if it becomes stalled and you really need to shut it down fast.
www.referjava.com
Why shouldn't you normally use the Thread.stop() method? Well, it is deprecated as of JDK1.2 because it can potentially leave the system in an unsafe state. If a thread
had a lock on an object (within a synchronized block), it might not release the object lock, causing problems at a later time. Note that this problem can affect older JVM implementations as well, JDK1.02 and JDK1.1 are not immune.
So while you shouldn't use Thread.stop() unless absolutely necessary, it is an acceptable way to kill a thread if it becomes stalled and you really need to shut it down fast.
www.referjava.com
What is an abstract class, and when should it be used?
Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods. Let's look at an example of an abstract class, and an abstract method.
Suppose we were modeling the behavior of animals, by creating a class hierachy that started with a base class called Animal. Animals are capable of doing different things like flying, digging and walking, but there are some common operations as well like eating and sleeping. Some common operations are performed by all animals, but in a different way as well. When an operation is performed in a different way, it is a good candidate for an abstract method (forcing subclasses to provide a custom implementation). Let's look at a very primitive Animal base class, which defines an abstract method for making a sound (such as a dog barking, a cow mooing, or a pig oinking).
public abstract Animal
{
public void eat(Food food)
{
// do something with food....
}
public void sleep(int hours)
{
try
{
// 1000 milliseconds * 60 seconds * 60 minutes * hours
Thread.sleep ( 1000 * 60 * 60 * hours);
}
catch (InterruptedException ie) { /* ignore */ }
}
public abstract void makeNoise();
}
Note that the abstract keyword is used to denote both an abstract method, and an abstract class. Now, any animal that wants to be instantiated (like a dog or cow) must implement the makeNoise method - otherwise it is impossible to create an instance of that class. Let's look at a Dog and Cow subclass that extends the Animal class.
public Dog extends Animal
{
public void makeNoise() { System.out.println ("Bark! Bark!"); }
}
public Cow extends Animal
{
public void makeNoise() { System.out.println ("Moo! Moo!"); }
}
Now you may be wondering why not declare an abstract class as an interface, and have the Dog and Cow implement the interface. Sure you could - but you'd also need to implement the eat and sleep methods. By using abstract classes, you can inherit the implementation of other (non-abstract) methods. You can't do that with interfaces - an interface cannot provide any method implementations.
www.referjava.com
Suppose we were modeling the behavior of animals, by creating a class hierachy that started with a base class called Animal. Animals are capable of doing different things like flying, digging and walking, but there are some common operations as well like eating and sleeping. Some common operations are performed by all animals, but in a different way as well. When an operation is performed in a different way, it is a good candidate for an abstract method (forcing subclasses to provide a custom implementation). Let's look at a very primitive Animal base class, which defines an abstract method for making a sound (such as a dog barking, a cow mooing, or a pig oinking).
public abstract Animal
{
public void eat(Food food)
{
// do something with food....
}
public void sleep(int hours)
{
try
{
// 1000 milliseconds * 60 seconds * 60 minutes * hours
Thread.sleep ( 1000 * 60 * 60 * hours);
}
catch (InterruptedException ie) { /* ignore */ }
}
public abstract void makeNoise();
}
Note that the abstract keyword is used to denote both an abstract method, and an abstract class. Now, any animal that wants to be instantiated (like a dog or cow) must implement the makeNoise method - otherwise it is impossible to create an instance of that class. Let's look at a Dog and Cow subclass that extends the Animal class.
public Dog extends Animal
{
public void makeNoise() { System.out.println ("Bark! Bark!"); }
}
public Cow extends Animal
{
public void makeNoise() { System.out.println ("Moo! Moo!"); }
}
Now you may be wondering why not declare an abstract class as an interface, and have the Dog and Cow implement the interface. Sure you could - but you'd also need to implement the eat and sleep methods. By using abstract classes, you can inherit the implementation of other (non-abstract) methods. You can't do that with interfaces - an interface cannot provide any method implementations.
www.referjava.com
What is a "magic number" in Java, and why does it sometimes go bad (referring to a bad magic number error when loading applets) ?
The class definition files (*.class) for Java applets are loaded over the network. Sometimes during the transmission of files, the connection may be aborted, or may be scrambled, causing class loading to fail. Sometimes when copying files over to a web server, they may become garbled or a disk error might occur. For this reason, special care is taken by the JVM and the class loader, to verify that classes are intact. One of the precautions is that every class definition contains at the beginning the magic number, a sequence of four bytes that identify a file as a Java class definition file.
For those curious to know what the magic number is, it is the hexadecimal number CAFEBABE, which is used by the class loader to see if a file is really a class definition file. Please don't ask me why it spells out cafebabe - my guess it was an attempt at humor.
www.referjava.com
For those curious to know what the magic number is, it is the hexadecimal number CAFEBABE, which is used by the class loader to see if a file is really a class definition file. Please don't ask me why it spells out cafebabe - my guess it was an attempt at humor.
www.referjava.com
I'm trying to compile a Java source file, and get the error message "bad command or filename". What did I do wrong?
This is a VERY frequently asked question. You need to add a path statement in your autoexec.bat file on windows to allow the javac.exe program to be found.
For example, if you installed java to the c:\java\ directory, you'd add the following to autoexec.bat
set path=%path%;c:\java\bin\
Hint: change the path to your installation dir (e.g. c:\jdk1.1.7\bin)
Remember to rerun the autoexec.bat file or reboot before trying to compile again, or the path setting will not be acted upon.
www.referjava.com
For example, if you installed java to the c:\java\ directory, you'd add the following to autoexec.bat
set path=%path%;c:\java\bin\
Hint: change the path to your installation dir (e.g. c:\jdk1.1.7\bin)
Remember to rerun the autoexec.bat file or reboot before trying to compile again, or the path setting will not be acted upon.
www.referjava.com
Subscribe to:
Posts (Atom)