| Joined: Dec 2002 Posts: 3,255 Likes: 3 UGN Elite | OP UGN Elite Joined: Dec 2002 Posts: 3,255 Likes: 3 | 1.) A Class A "class" is the same as a function, sub routine, sub program. A way to break up a program into small easier manageable parts. Also a class is the name of the file. for example Would be saved as MyCalss.java and compiled as MyClass.class 2.) A Method The processing portion. What I am thinking is, the action, the part od the code that dose something. You can have many methods in a class. An example would be public void myMethodThatReturnsNothing()
{
} | | | | Joined: Mar 2002 Posts: 1,136 UGN Elite Poster | UGN Elite Poster Joined: Mar 2002 Posts: 1,136 | A class is a compilation unit, and it combines both properties (member variables) and behavior (methods). The best way to think of it as a blueprint. You write one class, and from that, are able to create as many instances (objects) as you want. They all act the same, but represent a different entity. public class Dog {
private String name;
public Dog(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void bark() {
System.out.println("woof!");
}
} That class represents (duh!) a dog. In this case, all dogs have a name (the properties), and are able to bark (behavior). With this, you can do something like: Dog cobi = new Dog("Cobi");
Dog dog2 = new Dog("Fido");
cobi.bark();
dog2.bark(); So basically, a class allows you to create a blueprint for some entity. Then you use it to define particular instances, which act the same way, but have different state. It's a good way to model real-world objects. The best thing you can do is pick up a copy of Thinking in Java , by Bruce Eckel. And the beautiful thing is that he offers it for free on his website. So go download that, read through it, and you're well on your way. | | | | Joined: Dec 2002 Posts: 3,255 Likes: 3 UGN Elite | OP UGN Elite Joined: Dec 2002 Posts: 3,255 Likes: 3 | means this variable will not be accessible outside this class right? The Private part I mean... public Dog(String name) {
this.name = name;
} This is the Method, I get that... Where dose "this" come from? When called it will basically say name = Fido; ? I mean once the variable is filled it will be Fido? So what about this? cobi.bark();
dog2.bark(); Or when dog2.bark(); is called is that where Fido is assigned? What about cobi.bark();? I am so confused over this little blurb of code. /me slams head into sharp pencil. public String getName() {
return name;
} gets the name abviously, but "return name;" what dose this do? Same as print or println? Or maybe pass it on? public void bark() {
System.out.println("woof!");
} Obviously prints out "woof!" but what about the rest of the properties of the dog like the name. I know I am looking really retarded right now. But I swear it will click here soon. I downloaded the book and am reading it now. Thanks | | | | Joined: Mar 2002 Posts: 1,136 UGN Elite Poster | UGN Elite Poster Joined: Mar 2002 Posts: 1,136 | Originally posted by �int��: [QB] means this variable will not be accessible outside this class right? The Private part I mean... private means that any code that calls an instance of Dog can't call this particular member. You can use the variable "name" anywhere inside the Dog class. But if you were to write some code that did public Dog(String name) {
this.name = name;
} This is the Method, I get that... Where dose "this" come from? this is a variable which refers to the current object itself. Within a class, it basically means "myself." So when you type this.name, that means "the name variable within myself." The reason for using this there is because the member variable is named this, but so is the parameter for the constructor. If you don't use "this" in this case, then it doesn't do anything, because the parameter takes precedence. You use "this" here to specify that the member variable should be assigned the value of the parameter. When called it will basically say name = Fido; ? I mean once the variable is filled it will be Fido? After this statement, the member variable "name" will have the same value as the parameter passed in. So what about this? cobi.bark();
dog2.bark(); Or when dog2.bark(); is called is that where Fido is assigned? What about cobi.bark();? I am so confused over this little blurb of code. /me slams head into sharp pencil. The name gets assigned in the constructor. A constructor is a special method that's used to build objects. Unless you call a constructor, the object will be null. The bark() method just displays a line of text. Despite the fact that they both show the same output, they're actually being run on different objects. Try running the code public String getName() {
return name;
} gets the name abviously, but "return name;" what dose this do? Same as print or println? Or maybe pass it on? That just passes back the value of "name". That's called an accessor method, and it's how you let other parts of the program access the data of a particular object. All it does is give a String back...so you could do something like public void bark() {
System.out.println("woof!");
} Obviously prints out "woof!" but what about the rest of the properties of the dog like the name. All that does is display a line. You could change it to something like: public void bark() {
System.out.println("My name is " + name);
} And you'll see that each dog will output a different name. I wrote a short example above. | | | | Joined: Dec 2002 Posts: 3,255 Likes: 3 UGN Elite | OP UGN Elite Joined: Dec 2002 Posts: 3,255 Likes: 3 | C:\java progys>java Dog
Exception in thread "main" java.lang.NoSuchMethodError: main I am guessing there must be a main method in every class? | | | | Joined: Mar 2002 Posts: 1,136 UGN Elite Poster | UGN Elite Poster Joined: Mar 2002 Posts: 1,136 | No, there just has to be a main method in every program. Basically, if you do java myclass there has to be a main method in there. To test the Dog class out, you can create a main method in it, or create a new program. public class DogApp {
public static void main(String args[]) {
Dog d = new Dog("Snoop");
d.bark();
}
} C:\java progys>javac *.java
C:\java progys>java DogApp | | | | Joined: Dec 2002 Posts: 3,255 Likes: 3 UGN Elite | OP UGN Elite Joined: Dec 2002 Posts: 3,255 Likes: 3 | Okay, I got it working. Now could you please check to make sure I did this right? also please explain why I do not need a loop here.
public class Dog
{
public String name;
public static void main(String args[])
{
Dog d = new Dog("Snoop");
Dog cobi = new Dog("Cobi");
Dog dog2 = new Dog("Fido");
d.bark();
cobi.bark();
dog2.bark();
}
public Dog(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void bark()
{
System.out.println(this.name+" says woof!");
}
}
| | | | Joined: Dec 2002 Posts: 3,255 Likes: 3 UGN Elite | OP UGN Elite Joined: Dec 2002 Posts: 3,255 Likes: 3 | Okay. let me see if I get this "Dog" is an object or class. "d" "cobi" "dog2" These are all instances of this object. Kind of like cookie cut outs of a dog. public class Dog
//sets class/object name
{
public String name;//initialize variable name
public static void main(String args[]) //main method deffinition
{
Dog d = new Dog("Snoop");
Dog cobi = new Dog("Cobi");
Dog dog2 = new Dog("Fido");
//created new classes or instances of "Dog" and in
public Dog(String name)
{
//we said
Dog whatever = new Dog("Some_name");
^
Here is where we get the name from. | | | | Joined: Mar 2002 Posts: 1,136 UGN Elite Poster | UGN Elite Poster Joined: Mar 2002 Posts: 1,136 | Dog is the name of the class, and you're correct about d, cobi, and dog2 being instances. Technically, they're all references, and the actual objects reside in memory. But that's not all that important, really...just know that those are all objects. That code most definitely won't compile, and you shouldn't call the constructor within the constructor. public Dog(String name) {
Dog whatever = new Dog("Some_Name");
} does not work. You just need to assign the instance variable (this.name) to the value passed in (name). That assignment you made (Dog whatever =...) would be fine in the main() method. | | |
Posts: 259 Joined: June 2002
| | Forums41 Topics33,840 Posts68,858 Average Daily Posts0 | Members2,176 Most Online3,253 Jan 13th, 2020 | | | | | | | | | | | Doom 3 by Cyrez - 09/11/14 08:58 PM
| | | | | | | | | | | | | | | | | | |