Misc Topics in Java Programming
Installing JDK
- Go to this website to download the Java Development Kit (JDK), and we then install it.
- To check if the JDK is installed correctly, open Terminal on MacOS and run
javac --version
, which should showjavac <some version number>
- javac stands for Java Compiler.
Compliation in Java
- All code in Java goes in SomeName.java files, instead of having two different kinds of files like .h and .cpp files in C++. There are not separate header and implementation files, because declaration and implementation cannot be separated in Java.
- We compile all .java files individually as in we write javac SomeFile.java on the command line (i.e. you typically write this file by yourself, and this is your “source code”), and the java compiler turns this file into a SomeFile.class file (i.e. Java turns your source code into something that a machine can deal with). This file is a compiled version of SomeFile.java. Java doesn’t compile to machine code, so it doesn’t produce any .exe files or directly executable files. Instead, java compiles the source code into an intermediate form called Java bytecode, which runs on what’s called the Java Virtual Machine (JVM). What’s being run on the JVM is the .class file.
Many classes in one .java file
- You may get files like
SomeFile$SomeOtherName.class
as output when you compileSomeFile.java
because you actually get one class file for each class definition inside the .java file. An example will be when you have inner classes in your class definition. - In Java, generally, there is at most one
main(String[] args)
method per.java
file. Indeed, often times, in a large java project (i.e. a package that you compose), you may only need oneTest
class for testing your project, and in this whole project, the only class that has amain()
method will be this testing class.
Editors & IDEs for the Java Language
- XCode does not by default support Java.
- You can use Visual Studio Code for Java (Win, Mac, Linux). It is not an IDE! It is a text editor, but it does have a Java extension which is still fairly powerful.
- Eclipse is an IDE for Java.
Class
- Each
.java
file can contain only onepublic
class. This class is visible to every other class. - The file name of the
.java
file should be exactly the same as the name of thepubli
class it contains
Access Modifiers
- Class-Level Access Modifiers
- Member-Level Access Modifiers
Instance and Static members
Final members
Packaging
What is Packaging
- Packaging means you put a bunch of classes together into a package. You do this by including the command
pacakge pacakge_name;
at the beginning of the.java
file.
Why Packaging
- Firstly, it allows you to use package-private classes (without first packaging the .java file where the package-private class is defined, you cannot use package-private class because there is no related pacakge)
- Secondly, by pacakging classes in a package, you can explicitly use those classes (but certainly only the public fields and methods) in your own user-defined class. For example, the
java.lang
package provided classes that are fundamental to the design of the Java programming language, andMath
class (i.e.java.lang.Math
) is one of the important and frequently used classes.
Inheritance
Caveat on Conversions
- Suppose class
B
inherits classA
. By the rule of inheritance,B
inherits every protected and public method & members (not including any ofA
's constructors, asA
andB
essentially have different names; nor doesB
inherit package-private or private members inA
). - Moreover, due to polymorphism allowed by the inheritance structure, we can use a reference of type
A
to refer to aB
object. For example,A obj = new B();
. - Now assume
obj
has been created: Keep in mind thatB
may also have some members that are exclusive to itself (i.e. those are not inA
). SayB
has such a method calledpublic void bs_method()
. In this case, if you want to callbs_method()
fromobj
, you will need to firstly perform an explicit narrowing conversion (narrowing conversion must be explicit, or you get a compile error):((B) obj).bs_method();
. You will get a compile error if you do this:obj.bs_method();
. This is because you are calling the methodbs_method()
from a reference of typeA
, so the compiler will look for such a method in classA
's body, and it certainly won’t find it, so you get an compile error.
GUI with the Swing Package
GUI stands for Graphical User Interface. javax.swing
is a package that we can use to design interactive applications in Java.
Top Level Containers
- There are three classes in
swing
that serves as the top level containers in GUI:JFrame
,JDialog
, andJApplet
. We focus onJFrame
. - Top level containers are literally like the windows we typically see in computer applications i.e. the outermost frame we see when we open up an application program, and as they are the outermost frame, various GUI Components can be added to them. In fact, GUI Components must be part of a containment hierarchy (i.e. the structure of which GUI object contains which) in order to appear on the screen.
- However, one top level container cannot fit into another one as they are already the top level components of an application.
- Each top-level container has a content pane (of the class
Container
, accessed throughJFrame_object_name.getContentPane()
. GUI components must be added to this content pane, but we have many ways to achieve this, and some may be better than the others (seeJPanel
). Notice thatContainer
actually belongs to another packagejava.awt
. - A menu bar on (the top end of) a
JFrame
is optional. A menu bar is not a GUI component.
JFrame
JFrame
is a heavyweight container, so its physical appearance is heavily dependent on the underlying operation system (i.e. aJFrame
window on a Windows computer appears to be different than that on an IOS computer).
Adding GUI Components to JFrame
Instance Methods
setVisible(boolean)
add(Component)
setSize(int width, int height)
pack()
: resize the Jframe to fit a preferred size and layout of its sub-components.setResizable(boolean)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
: program does not exist when theJFrame
is closed. An alternative argument that is commonly used isJFrame.EXIT_ON_CLOSE
, which exits the java application if the user closes it.
JPanel
-
JPanel
is a container, but a lower-level one. -
It is also a lightweight container, which means it is managed not by the operation system but by the Java program.
-
Comparing with adding GUI components to the content pane or the frame directly, it is better to have GUI components added to JPanels, an then add these JPanels to a JFrame. Why?