You are on page 1of 18

JAVA Structures

Structures

• collection of objects having different data types


• Also known as records
• Each object in the structure is called a field.
Declaring a structure

class StructureName
{
Structure members
}
Example:
Example:

keyword
class Customer Structure name
{
int custnum;
int salary; Structure members
float commission;
};
How to declare Structure Variable?

Customer cust[ ] = new Customer[10];

Structure Name Structure variable name


Java Field
- a variable inside a class
For instance, in a class representing an employee, the
Employee class might contain the following fields:
• name
• position
• salary
• hiredDate
The corresponding Java class could be defined like this:

public class Employee {


String name;
String position;
int salary;
Date hiredDate;
}
Field Declaration Syntax

[access_modifier] [static] [final] type name [= initial value] ;

- Access modifier static, final and initial value are optional


- Only type and name are required
Java Field Access Modifiers

•private
•package
•protected
•public
private access modifier means that only code inside the class itself can
access this Java field.
package access modifier means that only code inside the class itself, or
other classes in the same package, can access the field. You don't
actually write the package modifier. By leaving out any access modifier,
the access modifier defaults to package scope.
protected access modifier is like the package modifier, except subclasses
of the class can also access the field, even if the subclass is not located in
the same package.
public access modifier means that the field can be accessed by all
classes in your application.
Example:
public class Customer {

private String email;


String position; //no modifier = package access modifier
protected String name;
public String city;

}
Static and Non-static Fields
static field- belongs to the class
public class Customer {

static String staticField1;

Static Java fields are located in the class, not in the instances
of the class.

Static fields are located in the class, so you don't need an Customer.staticField1 = "value";
instance of the class to access static fields.
System.out.println(Customer.staticField1);
Non-static Java fields
- located in the instances of the class. Each instance of the class can have its own values for
these fields.

public class Customer {

String field1;

To access a non-static field you need an instance of the class (an


object) on which you can access it.
Non-static Java fields are located in the
Customer customer = new Customer();
instances of the class
customer.field1 = "value";

System.out.println(customer.field1);
Final Fields
• cannot have its value changed, once assigned.
public class Customer {

final String field1 = "Fixed Value";

When you cannot change the value of a final field anyways, in many cases it makes sense to
also declare it static. That way it only exists in the class, not in every object too.

public class Customer {

static final String field1 = "Fixed Value";

}
Naming Java Fields
• used to refer to that field from your code.

Customer customer = new Customer();


customer.city = "New York";
System.out.println(customer.city);
Initial Field Value

public class Customer {

String customerType = "OnlineCustomer";

}
How to access structure members?

To access structure members, the operator used


is the dot operator denoted by (.)
Example:
System.out.println("Please enter the name of the student,
import java.util.*; press enter then enter his grade.");
import java.io.*; for(int i =0; I < student.length; i++)
{
class Record student[i] = new Record();
{ System.out.print(“Student “ + (i+1) + “:”);
int grade; student[i].studentName = input.next();
String studentName; System.out.print(“Grade “ + (i+1) + “:”);
} student[i].grade = input.nextInt();
}
public class structPractice
{ for( int i =0; i<student.length; i++)
public static void main(String args[]) {
{ System.out.println(student[i].studentName);
Scanner input = new Scanner(System.in); System.out.println(student[i].grade);
}
Record[] student = new Record[10];
}
}

You might also like