Email Subscription Form

Monday, April 15, 2013

Introduction to Java: Definitions

Another thing I struggled with when learning Java was what the difference was between a Class, an Object, and a Method.  Through personal experience and web searches, I have come up with this explanation:

A class is the basic building block in Java.  It consists of variables and methods.  Here's an example:

public class Cat {  //this is the class

//these are the variables in the Cat class
String name;
int age;
int weight;
String color;

//these are the methods in the Cat class
public void eat() {
}

public void sleep() {
}

public void meow() {
}

}

A variable is a little bit of memory that stores a value to be used in a program.  A String is a bit of text, like "Hello!" or "rainbow".  In the Cat class, the String variables we have are name (the name of the cat), and color (the color of the cat).  An int is an integer, like 7 or 13.  In the Cat class, the int variables we have are age (the age of the cat) and weight (the weight of the cat).

An Object is an instance of a class.  If we want our program to create and use a cat, we need to create a Cat object:

public void main() {  //this is the main method of our program

//this is where we create the new Cat Object
Cat Fluffy = new Cat;  //Fluffy is the variable name given to the Cat Object

}

method is a task or group of tasks.  If you are familiar with mathematical functions, you might want to think of a method as a function.  In the Cat class, the methods are eat(), sleep(), and meow().  The parentheses after the method name indicate what kind of parameter will be passed into the method.  In this case, eat(), sleep(), and meow() do not require a parameter.  In the next blog post I will give some examples of methods with parameters and how they work.



6 comments:

  1. ทาง PG-SLOT.GAME ได้จัดโปรโมชั่น PG Slot โปรเด็ดเอาใจนักเล่นที่สมัครเข้ามาเป็นสมาชิกกับทางเว็บของเรา ด้วยโปรสุดพิเศษ ฝาก-ถอนได้จริงเงื่อนไขไม่ยุ่งยาก พร้อมด้วยเจ้าหน้าทีคอยให้บริการ

    ReplyDelete
  2. เว็บตรงไม่ผ่านเอเย่นต์ พร้อมให้คุณได้เลือกอย่างหลากหลาย เล่นได้ทั้งบนแอปลิเคชั่น และเบราว์เซอร์ บริการท่านตลอด 24 ชั่วโมง เล่นได้แบบไม่มีขั้นต่ำ ทำได้ง่ายแบบไม่มีสะดุด อัพเดตเกมใหม่ให้คุณสามารถเล่นได้ตลอด ไม่มีเบื่อ PG สมัคร

    ReplyDelete
  3. เล่นง่าย แถมได้แจ็คพ็อตแบบไม่อั้น ด้วยการเดิมพันขั้นต่ำเพียง 1 บาท สมัคร PG เล่นสบาย ไม่มีขาดทุน เล่นได้เลยตอนนี้ที่เว็บไซต์ PGSLOT-AUTO ขนมากับสุดยอดกราฟฟิก แบบ 3มิติ ทำได้โดยไม่มีเทิร์น รับการแจกเครดิตฟรีได้เลยตอนนี้

    ReplyDelete
  4. ส่วนใหญ่ในสมัยนี่เป็น สล็อตเว็บตรง ที่มีผู้เล่นให้ความสนใจและเป็นที่นิยมกันเป็นอย่างมากเพราะมีเกมส์ใหม่ที่เรียกเงินและได้เงินมาใช้ได้จริงได้แก่แกมส์ Mahjong ways 2 เพราะเป็นเกมส์ที่มีภาพที่สวยและเงินโบนัสลงอย่างต่อเนื่องแถมยังมีตัวคุณให้กับลูกค้าอีกด้วย ลูกค้าหลายท่านสงสัยว่าทำไมเป็นรูปไพ่จีนหรือไพ่นกกระจอกเพราะตัวเกมส์ Mahjong ways 2 ได้แรงบัลดาลใจมากจากหนังKing of mahjong

    ReplyDelete

New Blog Location!

I've moved!  I've really enjoyed using Blogger for my blog, but it didn't integrate with my website in the way I wanted.  So I&#...