Search Java Tutorials

Custom Search

What You Will Want to Complete This Tutorial?

You must have at least 1st and 2nd pre-requirements to start the tute. Other requirements will require for last chapters only. These all are free to use and download!
  1. Java Development Kit Standard Edition (JDK / J2SE) Any Version(www.java.com)
  2. Any Text Editor (Note Pad quite enough or get Notepad++ from (http://notepad-plus.sourceforge.net)
  3. Java Run Time Environment (JRE) Any Version(www.java.com)
  4. Java Enterprise Edition (J2EE) for advanced developing (For last few chapters)(www.java.com)
  5. Java Wireless Toolkit for Mobile Application Development (For Mobile Application Development Chapters) (www.java.com)
  6. Tomcat Web Server to run Servlets. (www.tomcat.apache.org/)
Your operating system may be any version of Windows.
What will you learn (As a summary)?
  1. How to build console and windowed applications using Java
  2. How to build web page elements with Java Applets
  3. How to build Database Programs with Java
  4. How to build enterprise applications with Beans
  5. How to add Email functions to Java programs
  6. How to program smart devices (Phone, PDA etc…) with Java

Instance Variables in Java

The data components of a class are often referred to as the instance variables of that class. Also, class object attributes are often called fields to help distinguish them from other variables you might use.

This is the source class for ‘myObject’. In line 3 we have declared a instance variable (known as a class field, because it declares inside class block, not in a method). ‘setText’ method will set a value to strText, and ‘getText’ method will return the value in strText.
Save this class as “InstanceVariables.java”

public class InstanceVariables
{
String strText;

public void setText()
{
strText = "This is VariableUserMethod";
}

public string getText()
{
return strText
}
}


This is the class which uses “InstanceVariables.java” through the object “myObject”.

public class TextEdit
{
public static void main(String[] args)
{
InstanceVariables myObject = new InstanceVariables();
//without arguments
InstanceVariables myArgObject = new InstanceVariables("With Arguments");

System.out.println("Getting the value with myObject");
System.out.println("Value is:" + myObject.getText());

System.out.println("Getting the value with myNewObject");
System.out.println("Value is:" + myArgObject.getText());
}
}



This is the output:

Tip: An object is constructed by instantiating a class. The process of creating an object of a class is called as instantiation and the created object is called as an instance.

No comments: