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
Now from my understandings, isn't * a wildcard and wouldn't it import everything in java.awt in the first line?Code:import java.awt.*; import java.awt.event.*
2. Shortly after we have,
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.Code:public static void main(String[] args) { some code etc }
3. Last question I have is with this piece of code:
Now after WindowEvent, there is an e. Is this a variable? or like a parameter or something?Code:public void windowClosing(WindowEvent e) { System.exit(0); }
Thanks for any replies!
1).. i actually don't know myself :s
2) 'public' means other classes can see/use it.
Static = the class where this part is written in doesn't has to be instantiated to let this method being used.
Void. This method doesn't return something
example 1.
A class is defined with the "class" wordt, eg public class MyClass or class YourClass or private class PrivateClass.Code:public class MyClass{ private int year; public MyClass(int year){ this.year = year; } private void doubleYear(){ year*=2; } public int getYear(){ return year; } public static String whatIsThis(){ return "this class has only one attribute: year"; }
The constructor looks like 'public/private/protected <className> (...){ };
This will create an object of the class. Or some people may say "instantiate the class", thus creating an instance of this class.
This method is "private" So if you had a 2nd class looking like:Code:private void doubleYear(){ year*=2; }
This would fail, because the class "Second" does not has acces to the doubleYear() method as it's declared private.Code:public class Second{ MyClass test = new MyClass(); test.doubleYear(); //error here
The void means it returns nothing.
In the next method it does:
This one returns an integer + can be accessed from outside as it's public:Code:public int getYear(){ return year; }
Code:public class Second{ MyClass test = new MyClass(); int year = test.getYear();
Static methods may be somewhat more complex:So you don't need an object of the class to use it.Code:public static String whatIsThis(){ return "this class has only one attribute: year"; }
Note that in the previous 2 examples i always created an instance, using "new MyClass();" And then i accessed the methods with "test.xx()"
Static methods don't need an instance an can be called directly:You may have used Integer.parseInt() as static method allready without knowing.Code:public class Third{ String output = MyClass.whatIsThis(); System.out.println(output): }
3) the e is a variable that you can use to get more information of the windowEvent.
In a different case and you would have a KeyEvent e. You can use the e to look up which key of the keyboard exactly was pressed for example.
Tells you how useful college is...Originally Posted by Did
* is a wildcard character, but behaves a little differently than I think you yet understand it to. The wildcard will only import all of the classes available in the direct java.awt package, not any sub packages of java.awt, which happens to include event. Packages are divided in a hierarchical system that allows you to place packages in other packages. The import statement very carefully does NOT import other packages within a package when you use the * character, simply because it would do more harm than good. Imagine if someone said "import *"?Originally Posted by Did
That is not a class, that is a class method. There's quite a bit of abstract goings on involved with this, so I'll try to carefully explain it. If you look above that line of code you should see something along the lines of "public class NameOfClass" (insert the name, obviously), that's the actual class declaration, and everything that is within it is nested in the NameOfClass class. Classes may contain properties, methods, or other nested classes. The properties and methods may also be either static (named "class methods" for methods, or "static fields" for properties) or non-static (named "instance methods" for methods, or "instance variables" for properties), and their differences are important. Here's a class with one of each:Originally Posted by Did
So, now on to explain what the main class method is, which all Java programs must contain, and you are correct in assuming that "main" means the very first part of the Java program to be run.Code:public class Example { public static void classMethod() { // This method is a class method, which means it may be ran without an // instance of the class to run from. Because of this, non-static properties, // methods, and the this pointer are inaccessible from these methods. // Here's an example of using it: // Example.classMethod(); } public void instanceMethod() { // This method is only accessible from an instance of the class, IE after you make // a "new" Example class. For example: // Example e = new Example(); // e.instanceMethod(); } // The following is a static field. This means it also does not require an instance // of the Example class to be run and can be accessed from static methods. // For example: // Example.classProperty = 5; public static int classProperty; // The following is an instance variable, it can only be accessed when an instance // of the Example class exists. For example: // Example e = new Example(); // e.instanceProperty = 5; public int instanceProperty; private class NestedClass { // This is a nested class. It can be treated in every way like a normal class // with the marked exception that it may also be protected or private. // These are useful when you want a class that is intrinsically linked to the // containing class, and thus need access to it's private properties and // methods. This replaces "friend" access in C++. } }
I hope that all made sense!Code:public static void main(String[] args) { /* I'll go one at a time. "public" means that the method is accessible by objects that are not related to this class. It essentially means any part of the code has free access to this method and may run it at any time. You need to give main public access. "Static" means that this is a class method, and as such may be accessed without having an instance of the class in existence. The main method must be static, since there won't be an instance of the containing class when the program is first run. "Void" means that the method does not return any value. Most methods will return a value, but some do not and simply perform some sort modification to the state of the program, which is why void exists. The main method must be void, since the JRE does not expect a return value. "main" is the name of the method, and all Java programs must have one. "String[]" is an array of String classes, the String class is something you will quickly become familiar with. You may treat the string array parameter just like any other array of string. "args" is the name of the String class array parameter. A parameter is a data type or class reference that you pass to a method so that method may act upon that parameter and perform the programmed calculation in a different manner. */ }
That is actually pretty interesting, it's an overloaded method of the WindowListener interface. I'd like to see, in context, how this is applied, I'd imagine in the source code you're using it's from a temporary object called a "WindowAdapter". Please post more.Originally Posted by Did
To explain what "e" is, is that is both a parameter and a variable. It's also a class reference, a reference to a WindowEvent class. This is a callback method that refers to an event that is fired off when the window is closing, and the WindowEvent object has information regarding the old and new state of the window, the source object that fired the callback method from which the WindowEvent object was passed from (the source), and other identifying information. For the most part you can ignore this parameter unless you need to know what the source of the callback was, or if there's something about the state of the window you will need to know.
I tried to be thorough, hope that was enough to get you started.
Wow I changed my sig!
Exactly. But I'm also going to a very ****ty school. They only have 1 computer degree and that is Computer Tech, and I hate hardware with a passion. I'm only going because its completely free (FAFSA). I'm transferring out of it soon though, hopefully. I'm looking towards a computer science degree.Tells you how useful college is...
1.
Alright so understand the asterisk I believe; basically if i had
and say I used import.folder1 itd only import file1 and file2 and it wouldn't import anything in any other folders such as File3. (Based on a file structure and not on packages. So I can see if I understand correctly)Code:-- Folder1 ---File1 ---File2 ----Folder2 -----File3
2.
So Public, means other classes can use it..
Private means that only that specific class can use it.
In the code
are you creating the method doubleYear, and then calling it later? Also if it was public and not private, wouldn't it not work since its void and as you said void method doesn't return something?Code:private void doubleYear(){ year*=2; }
Here:
Is the method already created somewhere, and your just calling the method, and returning the variable year?Code:public int getYear(){ return year; }
If so is the getYear method in a package that I would have to import in the beginning?
I understand(I think) the static way of doing things. She never really explained "new" etc, so MyClass test = new MyClass(); kinda confuses me.
Fields/properties are also new to me and I don't quite understand them either. Are properties variables?
As far as the main method goes, we've had to write a few applets and I don't believe I saw a main method in them? Is there a reason for that?
The following is the code that you wanted me to post more of:
You both helped me out GREATLYCode:import java.awt.*; import java.awt.event.*; public class Buttons extends Frame implements ActionListener, ItemListener { Choice colors = new Choice(); public Buttons() { setLayout(new BorderLayout(20,5)); colors.add("Red"); colors.add("Yellow"); colors.add("Magenta"); colors.add("Cyan"); colors.add("White"); colors.addItemListener(this); Button Red = new Button("Red"); Button Yellow = new Button("Yellow"); Button Magenta = new Button("Magenta"); Button Cyan = new Button("Cyan"); //Button White = new Button("White"); add(Red, BorderLayout.NORTH); add(Yellow, BorderLayout.SOUTH); add(Magenta, BorderLayout.EAST); add(Cyan, BorderLayout.WEST); //add(White, BorderLayout.CENTER); add(colors, BorderLayout.CENTER); Red.addActionListener(this); Yellow.addActionListener(this); Magenta.addActionListener(this); Cyan.addActionListener(this); //White.addActionListener(this); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); } public static void main(String[] args) { Buttons f = new Buttons(); f.setTitle("Border Application"); f.setBounds(200,200,300,300); f.setVisible(true); f.setBackground(Color.red); } public void actionPerformed(ActionEvent e) { String arg = e.getActionCommand(); if (arg == "Red") setBackground(Color.red); if (arg == "Yellow") setBackground(Color.yellow); if (arg == "Magenta") setBackground(Color.magenta); if (arg == "Cyan") setBackground(Color.cyan); } public void itemStateChanged(ItemEvent ie) { String arg = colors.getSelectedItem(); if (arg == "Red") setBackground(Color.red); if (arg == "Yellow") setBackground(Color.yellow); if (arg == "Magenta") setBackground(Color.magenta); if (arg == "Cyan") setBackground(Color.cyan); if (arg == "White") setBackground(Color.white); } }, I understand a hell of a lot more than I once did. I spent a an hour or so just examining these posts.
Also, I know a bit a of perl and a very very minimal amount of php and other stuff, so I guess Im trying to understand Java by comparing things.
For example, methods to me seem like functions.
P.S. I just happen to be eating spaghetti and meatballs tonight..(Your avatar)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks