UGN Security Forums
My ProfileMember DirectoryLogin
Search our ForumsView our FAQView our Site Rules
View our CalendarView our Active TopicsGo to our Main Page

UGN Security Store
 

Network Sites UGN Security, The GoNix Initiative, Elite Web Gamers, Back of the Web, EveryDay Helper, VNC Web Design & Development
December
Su M Tu W Th F Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Sponsored Links
Latest Postings
Latest Reviews
Topic Options
Rate This Topic
#18515 - 03/23/05 10:30 PM Java True False(seeing if it is clicking)
§intå× Offline


*****

Registered: 12/03/02
Posts: 3255
Loc: Maryland
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()
{
}
_________________________
My New site OpenEyes

Top
Sponsored Links
      
#18516 - 03/24/05 06:37 PM Re: Java True False(seeing if it is clicking)
pergesu Offline
UGN Elite Poster

Registered: 03/14/02
Posts: 1136
Loc: Pimpin the Colorizzle
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.

Top
#18517 - 03/24/05 10:43 PM Re: Java True False(seeing if it is clicking)
§intå× Offline


*****

Registered: 12/03/02
Posts: 3255
Loc: Maryland
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
_________________________
My New site OpenEyes

Top
#18518 - 03/25/05 12:06 AM Re: Java True False(seeing if it is clicking)
pergesu Offline
UGN Elite Poster

Registered: 03/14/02
Posts: 1136
Loc: Pimpin the Colorizzle
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.

Top
#18519 - 03/25/05 04:53 AM Re: Java True False(seeing if it is clicking)
§intå× Offline


*****

Registered: 12/03/02
Posts: 3255
Loc: Maryland
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?
_________________________
My New site OpenEyes

Top
#18520 - 03/25/05 05:30 AM Re: Java True False(seeing if it is clicking)
pergesu Offline
UGN Elite Poster

Registered: 03/14/02
Posts: 1136
Loc: Pimpin the Colorizzle
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

Top
#18521 - 03/25/05 05:44 PM Re: Java True False(seeing if it is clicking)
§intå× Offline


*****

Registered: 12/03/02
Posts: 3255
Loc: Maryland
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!");
   	}
}
 
_________________________
My New site OpenEyes

Top
#18522 - 03/25/05 06:10 PM Re: Java True False(seeing if it is clicking)
§intå× Offline


*****

Registered: 12/03/02
Posts: 3255
Loc: Maryland
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.
_________________________
My New site OpenEyes

Top
#18523 - 03/26/05 12:18 PM Re: Java True False(seeing if it is clicking)
pergesu Offline
UGN Elite Poster

Registered: 03/14/02
Posts: 1136
Loc: Pimpin the Colorizzle
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.

Top

Moderator:  §intå×, Gremelin 
Featured Member
Registered: 02/28/02
Posts: 7193
Forum Stats
2152 Members
46 Forums
36187 Topics
71357 Posts

Max Online: 1567 @ 04/25/10 05:20 AM
Top Posters
UGN Security 29348
Gremelin 7193
§intå× 3255
SilentRage 1273
Ice 1146
pergesu 1136
Infinite 1041
jonconley 955
Girlie 908
unreal 860
Newest Members
cdefgh368568, HushHush, golqm, Tim050, Gecko666
2151 Registered Users
Who's Online
0 registered (), 387 Guests and 338 Spiders online.
Key: Admin, Global Mod, Mod
Latest News


Donate
  Get Firefox!
Get FireFox!