Computer Science Canada

Classes and Voids

Author:  MysticVegeta [ Thu May 05, 2005 4:25 pm ]
Post subject:  Classes and Voids

Can someone please tell me what a class is, since i saw everyprogram begins with a class, its not like turing where you can just start the code...
Also, what are "public static voids"...??
Also after declaring a variable, How do i "get" it? :S

I am a newbie to java and yes i am in Grade 9 and yes i have completed turing course and no I am not doing a java course right now. I like programming.

Author:  wtd [ Thu May 05, 2005 4:57 pm ]
Post subject: 

In Java, pretty much everything is an object. An object's behavior is defined by a class. Hence we use lots of classes.

Let's look at a simple class:

code:
class Foo
{
   public void bar()
   {
      System.out.println("bar");
   }
}


Now we can create a new Foo object.

code:
Foo f = new Foo();


And because the bar method is public, we can call it from outside the class.

code:
f.bar();


Now, what does "void" mean?

Well, void means that the method doesn't return anything.

What does static mean? Well, in the above we defined a "bar" method which we can call on the object. It doesn't always make sense to call methods on a specific object, so we have "static" methods which can be called on the class itself. Let's change our class.

code:
class Foo
{
   public static void bar()
   {
      System.out.println("bar");
   }
}


Now, we can call it like so:

code:
Foo.bar();

Author:  MysticVegeta [ Thu May 05, 2005 8:56 pm ]
Post subject: 

oh i get it now. And an applet is something created using java that can be used to compile on the websites? Like all the online java games and stuff?

Author:  Hikaru79 [ Thu May 05, 2005 9:02 pm ]
Post subject: 

MysticVegeta wrote:
oh i get it now. And an applet is something created using java that can be used to compile on the websites? Like all the online java games and stuff?


Yes, but an Applet is just a certain type of class. Everything in Java is an Object. And Applets don't *compile* on websites, they just run on them. But yeah, most online Java games and stuff are Applets (with the very rare exception being jws apps)

Author:  MysticVegeta [ Fri May 06, 2005 6:21 am ]
Post subject: 

thanks a lot


: