Few very basic Java questions.
Hey, I'm brand new to the forum; just signed up.
Anyway, Im taking a Java programming class right now in college, yet the teacher is horrible doesn't explain anything. She currently just gives us code that we copy and paste into text pad, compile and then run.
So because of this, it leaves everyone full of questions, tis why I need a few answers.
Also I'd appreciate it for the answers to be dumbed down a bit..
1. In the current program she has us writing we are importing 2 files
Code :
import java.awt.*;
import java.awt.event.*;
Now from my understandings, isn't * a wildcard and wouldn't it import everything in java.awt in the first line?
2. Shortly after we have,
Code :
public static void main(String[] args) { some code etc }
Does public mean that the following code is a class? Also are classes like functions as in I can call them later in the program? Also what exactly does static/void mean. I assume main means its the start of the program or main program.
3. Last question I have is with this piece of code:
Code :
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
Now after WindowEvent, there is an e. Is this a variable? or like a parameter or something?
Thanks for any replies!
Re: Few very basic Java questions.
The .* wildcard on the import statement will import all classes within the package, so all classes within java.awt package in your case, however this does not extend to subpackages so classes java.awt.event need to have a seperate import statement.
public is used to indicate the method has public accessiblity, i.e. any user of this class would be able to access this method, if the method were private only code within the class would be able to access the method, you should get a good Java book and read up on the basic OO principles regarding encapsulation to understand this and other methods of accessibility (such as protected and package), however it is important you understand it.
When a variable is declared with the static keyword, its called a “class variable”. All instances share the same copy of the variable. A class variable can be accessed directly with the class, without the need to create an instance.
Without the “static” keyword, it's called an “instance variable”, and each instance of the class has its own copy of the variable. Methods can also be declared with the static keyword. When a method is declared static, it can be called/used without having to create a object first. Methods declared with “static” keyword are called “class methods”. Otherwise they are “instance methods”.
the public static void main method should be declared if you wish to make your Java class runnable, this is the method the JVM will look for to run the class, if no such method exists then the class can not be run direct from the JVM but can be used be used by other classes within the JVM providing it is on the classpath.
WindowEvent e is a parameter sent to the WindowClosing method you can access this parameter within the scope of the method by using the e variable.
I would suggest you get hold of a good Java book, this one is excellent and will give you a good basic understanding:
Try 'Head First Java' by 'Kathy Sierra'
"http://www.amazon.co.uk/Head-First-Java-Kathy-Sierra/dp/0596009208/ref=sr_1_1?ie=UTF8&s=books&qid=1266056131&sr=8-1"
Re: Few very basic Java questions.
Alright, thanks, ill definitely get that book!