UGN Security
Posted By: §intå× Java True False(seeing if it is clicking) - 03/24/05 03:30 AM
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()
{
}
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.
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
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.
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?
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
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!");
   	}
}
 
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.
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.
© UGN Security Forum