Bright Java Tutorial

is brought to you by DoITBright LLC


Object Methods and Attributes

An object may commonly have methods and attributes. A method is a set of code that maybe invoked through it's given method name. For example, if we have a student object, it may commonly perform studying by reading a topic and writing something about it afterwards. We can group those tasks in a method called study. So, every time we invoke that student study method, it will perform those read and write tasks.
An attribute is the characteristic of an object. For example, a student will have a first name and last name.

  package com.example.core;

  public class ObjectMethodsAndAttribute {

    //String variable that is declared as an attribute
	String stringAttribute;
	//long variable that is declared as an attribute
	long longAttribute;
	 * A method is used to perform a group of related tasks. 
	 * It is used to perform an operation.
	public void printAttributes() {
	  System.out.println("stringAttribute = " 
	    + stringAttribute);
	  System.out.println("longAttribute = " 
	    + longAttribute);
	public static void main(String[] args) {
	  ObjectMethodsAndAttribute obj1 
	    = new ObjectMethodsAndAttribute();
	  obj1.longAttribute = 100;
	  obj1.stringAttribute = "abc";
	  ObjectMethodsAndAttribute obj2 
	    = new ObjectMethodsAndAttribute();
	  obj2.longAttribute = 200;
	  obj2.stringAttribute = "def";
	  //print each object attributes
	   * As you may have seen, each object contains 
	   * different data. Just like a house. Even 2 
	   * houses are built based on the same blue 
	   * print, it may have been painted with different 
	   * colors. Using the student example, even if 2 
	   * students are both humans and are students, 
	   * and both of them have  first and last names, it 
	   * does not mean they share the same first name and/or
	   * share the same last name. This is what we call
	   * in lay man's terms as individuality. More so,
	   * even they perform the same steps in studying,
	   * the results will also vary depending on the
	   * data provided and other factors. 

Back    Next