JAVA Structures
JAVA Structures
Structures
class StructureName
{
Structure members
}
Example:
Example:
keyword
class Customer Structure name
{
int custnum;
int salary; Structure members
float commission;
};
How to declare Structure Variable?
•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 {
}
Static and Non-static Fields
static field- belongs to the class
public class Customer {
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.
String field1;
System.out.println(customer.field1);
Final Fields
• cannot have its value changed, once assigned.
public class Customer {
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.
}
Naming Java Fields
• used to refer to that field from your code.
}
How to access structure members?