Previous Thread
Next Thread
Print Thread
Rate Thread
Joined: Dec 2002
Posts: 3,255
Likes: 3
UGN Elite
OP Offline
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

Code
public class MyClass
{
} 
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

Code
public void myMethodThatReturnsNothing()
{
}

Joined: Mar 2002
Posts: 1,136
P
UGN Elite Poster
Offline
UGN Elite Poster
P
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.

Code
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:
Code
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 Offline
UGN Elite
Joined: Dec 2002
Posts: 3,255
Likes: 3
Code
private String name;
means this variable will not be accessible outside this class right? The Private part I mean...


Code
   public Dog(String name) {
      this.name = name;
   }
This is the Method, I get that...

Code
this.name = name;
Where dose "this" come from?

Code
.name = name;
When called it will basically say
name = Fido; ?

I mean once the variable is filled it will be Fido?

So what about this?
Code
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.

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?

Code
   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
P
UGN Elite Poster
Offline
UGN Elite Poster
P
Joined: Mar 2002
Posts: 1,136
Quote:
Originally posted by �int��:
[QB]
Code
private String name;
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
Quote:

Code
   public Dog(String name) {
      this.name = name;
   }
This is the Method, I get that...

Code
this.name = name;
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.

Quote:

Code
.name = name;
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.

Quote:

So what about this?
Code
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
Quote:

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
Quote:

Code
   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:
Code
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 Offline
UGN Elite
Joined: Dec 2002
Posts: 3,255
Likes: 3
Code
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
P
UGN Elite Poster
Offline
UGN Elite Poster
P
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.

Code
public class DogApp {
   public static void main(String args[]) {
      Dog d = new Dog("Snoop");
      d.bark();
   }
}
Code
C:\java progys>javac *.java
C:\java progys>java DogApp

Joined: Dec 2002
Posts: 3,255
Likes: 3
UGN Elite
OP Offline
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.

Code
 
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 Offline
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.

Code
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
P
UGN Elite Poster
Offline
UGN Elite Poster
P
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.
Code
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.


Link Copied to Clipboard
Member Spotlight
Phatal
Phatal
Houston, TX
Posts: 298
Joined: April 2004
Forum Statistics
Forums41
Topics33,840
Posts68,858
Average Daily Posts1
Members2,176
Most Online3,253
Jan 13th, 2020
Latest Postings
Where and how do you torrent?
by danni75 - 03/01/24 05:58 AM
Animation,
by JohanKaariainen - 08/15/19 01:18 AM
Blackbeard.....
by Gremelin - 10/03/18 07:02 PM
my old account still exists!
by Crime - 08/10/18 02:47 PM
Okay WTF?
by HenryMiring - 09/27/17 01:45 AM
The History Thread...
by Gremelin - 08/11/17 12:11 PM
My friend NEEDS your HELP!
by Lena01 - 07/21/17 12:06 AM
I'm having fun with this guy.
by gabithompson730 - 07/20/17 01:50 AM
I want to upgrade my phone
by gabithompson730 - 07/20/17 01:49 AM
Doom 3
by Cyrez - 09/11/14 08:58 PM
Amazon Gift Card Generator/KeyGen?te
by Gecko666 - 08/22/14 09:21 AM
AIM scene 99-03
by lavos - 09/02/13 08:06 AM
Planetside 2
by Crime - 03/04/13 07:10 AM
Beta Testers Wanted
by Crime - 03/04/13 06:55 AM
Hello Everyone
by Gremelin - 02/12/12 06:01 PM
Tracfone ESN Generator
by Zanvin Green - 01/18/12 01:31 PM
Python 3 issue
by Testing - 12/17/11 09:28 PM
tracfone airtime
by Drache86 - 07/30/11 03:37 AM
Backdoors and the Infinite
by ZeroCoolStar - 07/10/11 03:52 AM
HackThisZIne #12 Releaseed!
by Pipat2 - 04/28/11 09:20 PM
gang wars? l33t-wars?
by Gremelin - 04/28/11 05:56 AM
Consolidate Forums
by diggin2deep - 04/21/11 10:02 AM
LAN Hacking Noob
by Gremelin - 03/12/11 12:42 AM
Top Posters
UGN Security 41,392
Gremelin 7,203
§intå× 3,255
SilentRage 1,273
Ice 1,146
pergesu 1,136
Infinite 1,041
jonconley 955
Girlie 908
unreal 860
Top Likes Received
Ghost 2
Cyrez 1
Girlie 1
unreal 1
Crime 1
Powered by UBB.threads™ PHP Forum Software 7.7.5