Classes And Instances In Java

In Java, each object is a thing, which is something (that means: it has an invariable identity, with which it is distinguishable from all other objects), has something (that means: it possesses certain properties) and can do something (that means: certain methods belong to it). One can illustrate this concretely, for example, with a ball: A ball has a certain shape, a certain size, a color, a characteristic surface, … and it can roll, you can shoot it away with your foot, throw it with your hand, it can be deformed, it can break, … Non-Static Classes The common properties and methods of similar objects are stored within a non-static class. class (cf. Static classes). Non-static classes can be thought of as a kind of template or blueprint for creating objects. For example, if Example is such a class, then Example has (at least) one constructor that can be used to create objects of type Example. An object created with a constructor is called an instance of the respective class. The possibility (and necessity) in the source code of a Java program source code of a Java program is the prerequisite for being able to distinguish strictly between theoretical concept and concrete implementation, in short: the Java programming language makes possible Abstraction. Only with an object, thus an instance of a class, the abstractly defined components of the class are realized. The characteristics of an instance obj of a class are called Attributes. Each attribute is represented in the definition of the class by a variable of suitable type or by a suitable object (within obj). The totality of the values stored in these variables together with all objects present within obj and their current properties determine the current state of the object obj. The instance of a class is completely characterized by its attributes and its methods. Instantiating classes In the program line Example example = new Example(); example is the identifier of a variable that refers to the object created by the constructor Example() of the class Example. To put it nicely: the newly created object is referenced by the variable pointing to it pointing to it. By means of the reference variable example you can “address” the corresponding object. (An object can have several references). The program line just explained is a summary of the following two program lines and, strictly speaking, contains three things: // declaring the reference variable example: Example example; // instantiating the class Example; // referencing the created object by example: example = new example(); Static classes Static classes are classes that cannot be instantiated. This means that calling a method and accessing an attribute defined in such a class can only be done via the class itself. In short: all methods of a static class are class methods and all attributes of a static class are class attributes. Class methods and attributes are declared using the keyword static (cf. Definition of methods). The static class Math provides methods that can be used for mathematical calculations. The static class System has the PrintStream out, which makes it possible to output strings and values of variables in a console window with its methods print() and println() (→ Java project HelloWorld). The InputStream System.in is responsible, among other things, for input via the keyboard. Start classes “Start class” is to be called that class of a Java project which contains the method main and with this ensures that the program existing with this project can be started. There may be only one start class in each project. In principle, any class in a project can be made the start class by adding the main method. However, in this tutorial, the start class of the respective project (whose name is always identical to the project name) has no other task besides starting the program. Definition of classes The definition of a class begins (at least) with the keyword class, immediately followed by the class name. The class name must be a permitted identifier, which should also begin with a capital letter. The class name must be preceded by modifiers that specify the properties of the class. If an Example class is a subclass of an existing SuperExample class, then the class name is followed by extends SuperExample. In the case that the method(s) of an interface named InterfaceExmpl are to be implemented, the following is also added implements InterfaceExmpl is added. Finally, the definition of the attributes as well as methods of the class follows within curly braces. A formal example: public class Example extends SuperExample { // Definition of variables ... // Definition of methods ... } A class is a template used to create any number of objects with similar properties. It includes all common properties of the objects derived from it. As class (class) is the definition of an idea, a concept, a type of objects. As object (object), copy or instance refers to a concrete manifestation of an object, i.e. a piece from the set of objects of this class. Examples: Hubert Partl and Monika Kleiber are objects of the class Employee. The University of Natural Resources and Applied Life Sciences is an object of the class University. The simpler the classes are chosen, the better. Complex objects can be composed of simpler objects (e.g. a bookshelf contains books, a book contains pages, an atlas contains maps). Special complicated properties can be traced back to basic simple properties (inheritance, e.g. an atlas is a special kind of book). A Java file corresponds to a Java class and always starts with a class definition, i.e. with the keyword “class” and a capitalized class name. Everything else is then inside the curly braces of a class. Example:

public class Person { ... statements ... } 

When a Java program is started, the JVM looks for the “main method” in the Java class specified at startup and executes its statements. Example:

public class PersonTesten { public static void main(String[] args) { ... statements ... } } 

A Java class is a template (blueprint) for creating concrete objects. An object is instantiated by a class, one speaks then of the instance of a class. Example: with the help of a class “person data” different person objects can be created.

A class contains

  • Instance variables (properties or states)
  • Methods (behavior or actions)

An instantiated object

  • knows things (object state)
  • does things (object behavior)
A class “Person” could contain the following instance variables and methods:
Class: Person --------------------------------------- Instance name variables: address phone --------------------------------------- Methods: setName readName setAddress readAddress setPhone readPhone 

Simplified class “Person” as a template for objects:

public class Person { private String name; public void setName(String name){ this.name=name; } public String getName(){ return this.name; } } 

The class definition is introduced by “class” and a capitalized class name, followed by instance variables and methods in curly braces. Here the variable “name” of type string is set to “private”, it cannot be changed from outside because of “private”. To change it, the methods setName and getName are offered, which are “public” (i.e. can be used from outside). To create objects from a class, a variable of the type of the class must be declared and a newly created object must be assigned to it with the help of the new operator. So first a normal variable declaration, where instead of the primitive type the type name of the self-defined class is chosen. Then create a new instance using the new operator. Test class “PersonTest” to create two person objects:

public class PersonTest { public static void main(String[] args) { Person p1=new Person(); Person p2=new Person(); p1.setName("Mr. A"); p2.setName("Mr. B"); System.out.println("Name of Person 1 is: "+p1.getName()); System.out.println("Name of Person 2 is: "+p2.getName()); } } 

When the class is started, two new Person objects are created in the main method. For this purpose, two instance variables p1 and p2 of the Person class type are declared and assigned to the Person object using the new operator. The point operator “.” is then used to access the methods of the objects (setName and getName). Notice: Instantiated objects are stored on the Java heap. The JVM (Java Virtual Machine) manages this memory area, it creates new objects there and deletes objects that are no longer needed. Object variables like “p1” are stored as references. You can imagine this like a remote control pointing to the heap and containing buttons for all methods (get, set) of an object. Classes And Instances In Java.




Leave a comment

Your email address will not be published. Required fields are marked *