Home Java Training Fields and Methods in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Fields and Methods in Java

Advertisement
Here we have discussed about the Fields and Methods in Java with examples and video tutorial.

In this section we are discussing about Fields and Methods in Java. Fields or variables of class and Methods of a class are important while writing the Java classes. Fields can be private, protected and public.

private fields cannot be accessed outside class. public fields can be accessed anywhere. protected class cal be accessed by subclass.

Fields and Methods can also be declared static and final. When declared final, method cannot be overridden and field can be assigned a value only once.

We have also provided Video tutorial for Fields and methods in Java that will help beginners in Java to understand the topic easily and quickly. The video explains in detail the use of fields in Java, methods in Java and variables in Java.

Variables of a Class

There are three types of variables:

1. Local variables

Local Variables are defined inside methods, constructors or blocks. These variables are declared and initialized within the method and are destroyed when the method completes.

Local Variables

Here is the video tutorial of Field and Methods in Java

Local variables are declared within the body of a function, its life is the time when method is being executed. Once method completes it execution the local variable is also removed and destroyed by JVM. Local variables are very useful if you have to perform some work in the method and don't want the temporary data.

Advertisement

Following is an example of local variable:

public class Test{ 
public void pupAge(){
int age = 0;
age = age + 7;
System.out.println("Puppy age is : " + age);
}

public static void main(String args[]){
Test test = new Test();
test.pupAge();
}
}

2. Instance Variables

Instance variables are instantiated when the class is loaded. Though they are declared outside a method they can be accessed from inside it.

Instance Variables

Following is an example of instance variable:

public class Mainclass {
int myage;
public Mainclass(String name) {
System.out.println("Name :" + name);
}
void setAge(int age) {
myage = age;
}
int getAge() {
System.out.println("Age :" + myage);
return myage;
}
public static void main(String[] args) {
Mainclass mc = new Mainclass("Ankit");
mc.setAge(24); mc.getAge();
System.out.println("Fetched Value via object :" + mc.myage);
}
}

3. Class Variables:

Class variables are declared within a class, outside any method, with the static keyword.

Class Variables

Following is an example of Class variable:

public class StaticDemo {
static int x = 3;
static int y;

static void display(int w) {
System.out.println("w = " + w);
System.out.println("x = " + x);
System.out.println("y = " + y);
}
static {
System.out.println("Inside Static block");
y = x * 4;
}

public static void main(String args[]) {
display(21);
}
}

Methods of a Class

A Java method is a collection of statements that are grouped together to perform an operation.

Methods of a Class

Syntax:

modifier returnValueType methodName(list of parameters) {
// Method body;
}

Example of a method of a class:

public int max(int num1, int num2) {
int result;
if (num1 > num2)
result = num1;
else
result = num2;

return result;
}

Instance variables and methods are accessed via created objects.

Syntax

Syntax:

/* Creating an object */
ObjectReference = new Constructor();

/* Calling a variable */
ObjectReference.variableName;

/* Call a class method */
ObjectReference.MethodName();

Example:

Example

public class DisplayGrade {
public static void main(String[] args) {
DisplayGrade dg=new DisplayGrade();
dg.printGrade(78.5);
}
public static void printGrade(double score) {
if (score >= 90.0) {System.out.println('A');}
else if (score >= 80.0) {System.out.println('B');}
else if (score >= 70.0) {System.out.println('C');}
else if (score >= 60.0) {System.out.println('D');}
else {System.out.println('F');}
}
}
Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 10, 2013

Ask Questions?    Discuss: Fields and Methods in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com