Sunday, September 18, 2011
Java Notes
Java Notes:
- one public class per file and the filename should be named after the public class name
- The top level class cannot be private or protected
- Multiple classes can be declared in a file but only one can be Public.
If you ever are in a situation where you need to use two classes with the same name but
in different packages, then using imports does not work. You will need to refer to each class
by their fully qualified name in your source file. The following code compiles successfully:
public class FullyQualifiedDemo {
public javax.management.AttributeList a1;
public javax.swing.text.html.parser.AttributeList a2;
}
Example: Take note of the command line arguments (dir structure) where it can compile and where it can run from
Code:
Message.java:
package org.rakesh;
public class Message {
public void PrintMessage(String msg) {
System.out.println(msg);
}
}
TestMessage.java:
package org.rakesh.test;
import org.rakesh.Message;
public class TestMessage {
public static void main(String[] args) {
Message m;
m = new Message();
m.PrintMessage("Rakesh Prajapati");
}
}
NOTE:
(no other dir path other than below could compile or run)
To Compile:
bash-4.1$ pwd
/home/oracle/workspace/learning/src
bash-4.1$ javac -d ../bin Message.java
bash-4.1$ pwd
/home/oracle/workspace/learning/src
bash-4.1$ javac -cp ../bin -d ../bin TestMessage.java
To Run:
bash-4.1$ pwd
/home/oracle/workspace/learning/bin
bash-4.1$ java org/rakesh/test/TestMessage
Rakesh Prajapati
Subscribe to:
Post Comments (Atom)
Followers
About Me
- reiki
- Torrance, CA, United States
No comments:
Post a Comment