How to Make a Class That Reads a File in Java
Coffee - Object and Classes
Java is an Object-Oriented Language. Every bit a language that has the Object-Oriented characteristic, Coffee supports the following fundamental concepts −
- Polymorphism
- Inheritance
- Encapsulation
- Abstraction
- Classes
- Objects
- Example
- Method
- Bulletin Passing
In this chapter, we will look into the concepts - Classes and Objects.
-
Object − Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an example of a course.
-
Class − A class can be divers every bit a template/blueprint that describes the behavior/state that the object of its blazon support.
Objects in Java
Let us now look deep into what are objects. If nosotros consider the real-globe, we can find many objects around us, cars, dogs, humans, etc. All these objects accept a country and a behavior.
If we consider a dog, then its land is - name, breed, color, and the behavior is - barking, wagging the tail, running.
If yous compare the software object with a real-world object, they take very like characteristics.
Software objects also have a land and a beliefs. A software object's state is stored in fields and behavior is shown via methods.
So in software evolution, methods operate on the internal state of an object and the object-to-object communication is washed via methods.
Classes in Coffee
A class is a blueprint from which private objects are created.
Post-obit is a sample of a class.
Instance
public class Domestic dog { String breed; int historic period; String color; void barking() { } void hungry() { } void sleeping() { } } A class can comprise any of the following variable types.
-
Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized inside the method and the variable will be destroyed when the method has completed.
-
Example variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can exist accessed from inside any method, constructor or blocks of that particular grade.
-
Class variables − Class variables are variables declared within a course, outside any method, with the static keyword.
A class can accept any number of methods to access the value of diverse kinds of methods. In the above instance, barking(), hungry() and sleeping() are methods.
Following are some of the important topics that need to be discussed when looking into classes of the Java Language.
Constructors
When discussing well-nigh classes, one of the most important sub topic would exist constructors. Every form has a constructor. If we exercise non explicitly write a constructor for a class, the Java compiler builds a default constructor for that class.
Each time a new object is created, at to the lowest degree one constructor will be invoked. The chief dominion of constructors is that they should accept the same name as the class. A course can have more than ane constructor.
Following is an example of a constructor −
Example
public class Puppy { public Puppy() { } public Puppy(Cord name) { // This constructor has one parameter, proper name. } } Java besides supports Singleton Classes where y'all would exist able to create only one example of a class.
Note − Nosotros have two unlike types of constructors. We are going to hash out constructors in item in the subsequent chapters.
Creating an Object
Every bit mentioned previously, a class provides the blueprints for objects. And then basically, an object is created from a form. In Java, the new keyword is used to create new objects.
At that place are three steps when creating an object from a class −
-
Declaration − A variable declaration with a variable proper noun with an object type.
-
Instantiation − The 'new' keyword is used to create the object.
-
Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
Following is an instance of creating an object −
Example
public class Puppy { public Puppy(Cord proper name) { // This constructor has one parameter, proper name. System.out.println("Passed Name is :" + name ); } public static void main(String []args) { // Post-obit statement would create an object myPuppy Puppy myPuppy = new Puppy( "tommy" ); } } If we compile and run the above program, then it will produce the post-obit consequence −
Output
Passed Name is :tommy
Accessing Instance Variables and Methods
Instance variables and methods are accessed via created objects. To access an case variable, following is the fully qualified path −
/* First create an object */ ObjectReference = new Constructor(); /* At present telephone call a variable equally follows */ ObjectReference.variableName; /* Now you can phone call a class method as follows */ ObjectReference.MethodName();
Case
This example explains how to admission instance variables and methods of a course.
public class Puppy { int puppyAge; public Puppy(String proper noun) { // This constructor has one parameter, name. System.out.println("Proper name chosen is :" + name ); } public void setAge( int historic period ) { puppyAge = age; } public int getAge( ) { System.out.println("Puppy's age is :" + puppyAge ); return puppyAge; } public static void master(String []args) { /* Object cosmos */ Puppy myPuppy = new Puppy( "tommy" ); /* Telephone call class method to fix puppy's age */ myPuppy.setAge( ii ); /* Telephone call another class method to become puppy's age */ myPuppy.getAge( ); /* You can access example variable equally follows every bit well */ Organization.out.println("Variable Value :" + myPuppy.puppyAge ); } } If we compile and run the higher up program, so it will produce the post-obit upshot −
Output
Proper noun chosen is :tommy Puppy's age is :two Variable Value :2
Source File Declaration Rules
As the last part of this section, allow'south now look into the source file declaration rules. These rules are essential when declaring classes, import statements and package statements in a source file.
-
There can be only one public class per source file.
-
A source file tin can have multiple non-public classes.
-
The public class name should be the name of the source file as well which should be appended past .coffee at the finish. For example: the class name is public class Employee{} so the source file should be every bit Employee.java.
-
If the class is defined inside a package, then the package argument should be the first statement in the source file.
-
If import statements are present, then they must be written between the package statement and the class announcement. If there are no parcel statements, then the import statement should exist the first line in the source file.
-
Import and package statements volition imply to all the classes present in the source file. It is not possible to declare different import and/or bundle statements to dissimilar classes in the source file.
Classes have several access levels and there are different types of classes; abstract classes, final classes, etc. We will be explaining nearly all these in the access modifiers chapter.
Apart from the higher up mentioned types of classes, Java also has some special classes called Inner classes and Bearding classes.
Java Packet
In elementary words, it is a way of categorizing the classes and interfaces. When developing applications in Java, hundreds of classes and interfaces volition be written, therefore categorizing these classes is a must equally well every bit makes life much easier.
Import Statements
In Java if a fully qualified name, which includes the package and the class proper noun is given, and so the compiler tin can hands locate the source lawmaking or classes. Import statement is a way of giving the proper location for the compiler to find that particular grade.
For case, the following line would ask the compiler to load all the classes available in directory java_installation/java/io −
import java.io.*;
A Simple Example Study
For our case report, we will exist creating two classes. They are Employee and EmployeeTest.
First open up notepad and add together the following code. Think this is the Employee class and the class is a public grade. Now, save this source file with the name Employee.java.
The Employee form has iv instance variables - name, age, designation and salary. The class has i explicitly defined constructor, which takes a parameter.
Case
import java.io.*; public class Employee { String name; int age; String designation; double salary; // This is the constructor of the class Employee public Employee(String name) { this.name = proper noun; } // Assign the age of the Employee to the variable age. public void empAge(int empAge) { historic period = empAge; } /* Assign the designation to the variable designation.*/ public void empDesignation(String empDesig) { designation = empDesig; } /* Assign the bacon to the variable bacon.*/ public void empSalary(double empSalary) { salary = empSalary; } /* Print the Employee details */ public void printEmployee() { System.out.println("Name:"+ name ); Organisation.out.println("Age:" + age ); Organization.out.println("Designation:" + designation ); System.out.println("Bacon:" + salary); } } As mentioned previously in this tutorial, processing starts from the master method. Therefore, in order for u.s.a. to run this Employee form there should be a master method and objects should be created. We will be creating a carve up form for these tasks.
Following is the EmployeeTest grade, which creates two instances of the class Employee and invokes the methods for each object to assign values for each variable.
Save the following code in EmployeeTest.java file.
import java.io.*; public form EmployeeTest { public static void main(Cord args[]) { /* Create ii objects using constructor */ Employee empOne = new Employee("James Smith"); Employee empTwo = new Employee("Mary Anne"); // Invoking methods for each object created empOne.empAge(26); empOne.empDesignation("Senior Software Engineer"); empOne.empSalary(1000); empOne.printEmployee(); empTwo.empAge(21); empTwo.empDesignation("Software Engineer"); empTwo.empSalary(500); empTwo.printEmployee(); } } Now, compile both the classes and so run EmployeeTest to run across the result as follows −
Output
C:\> javac Employee.java C:\> javac EmployeeTest.java C:\> coffee EmployeeTest Name:James Smith Historic period:26 Designation:Senior Software Engineer Salary:1000.0 Name:Mary Anne Age:21 Designation:Software Engineer Salary:500.0
What is Side by side?
In the side by side session, nosotros volition discuss the basic information types in Java and how they tin be used when developing Java applications.
Useful Video Courses
Video
Video
Video
Video
Video
How to Make a Class That Reads a File in Java
Source: https://www.tutorialspoint.com/java/java_object_classes.htm
Post a Comment for "How to Make a Class That Reads a File in Java"