Foreach with User defined class

// user defined class
class UserClass1 {
	String code, name;
	// default constructor
	public UserClass1() {	}
	// parameterized constructor
	public UserClass1(String code, String name) {
		this.code = code;
		this.name = name;
	}
	// override toString method
	@Override
	public String toString() {
		return code + "\t" + name;
	}
}
public class ForeeachUserClass {
	public static void main(String[] args) {
		// array object of user defined class with sizes
		UserClass1 obj[] = new UserClass1[3];
		obj[0] = new UserClass1("A001", "Ram kumar");
		obj[1] = new UserClass1("A002", "Kundan kumar");
		obj[2] = new UserClass1("A003", "Manoj kumar");
		// user foreach
		for (UserClass1 userClass1 : obj) {
			System.out.println(userClass1);
		}
	}
}
/* output - 
A001	Ram kumar
A002	Kundan kumar
A003	Manoj kumar */

Download

Java Basics Examples

Default constructor example
Parameterized constructor example
Call other constructor
Command Line argument example
Display sum of Command Line arguments
Foreeach example with int data type [ Only in JDK 1.5 ]
Foreeach example with String data type [ Only in JDK 1.5 ]
Foreeach example with user defiend [ Only in JDK 1.5 ]
Simple Inheritance
Call default constructor in Inheritance
Call parameterized constructor in Inheritance
Java Beans example
Overload method in one class
Overload method in inheritance
Override example , super keyword
super keyword example
override toString method


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code