Posts

Showing posts from January, 2021

Exercises - Creating Java objects and methods

Image
  13. Exercises - Creating Java objects and methods 13.1. Create a Person class and instantiate it Create a new Java project called  com.vogella.javastarter.exercises1  and a package with the same name. Create a class called  Person . Add three instance variables to it, one for storing the first name of the person, one for storing the last name and one for storing the age of the Person. Use the constructor of the  Person object to set the values to some default value. Add a  toString  method as described by the following coding and solve the TODO. This method is used to convert the object to a String representation. @Override public String toString () { // TODO replace "" with the following: // firstName + " " + lastName return "" ; } Create a new class called  Main  with a  public static void main(String[] args) . In this method create an instance of the  Person  class. 13.2. Use constructor Add a constructor to your  Person  class wh

Integrated Development Environment

Image
  12. Integrated Development Environment The previous chapter explained how to create and compile a Java program on the command line. A Java Integrated Development Environment (IDE) provides lots of ease of use functionality for creating Java programs. There are other very powerful IDEs available, for example, the Eclipse IDE. For an introduction on how to use the Eclipse IDE please see  Eclipse IDE Tutorial . The remaining description uses the phrase:  "Create a Java project called…​". This refers to creating a Java project in Eclipse. If you are using a different IDE, please follow the required steps in that IDE.

More Java language constructs

Image
  10. More Java language constructs 10.1. Class methods and class variables Class methods and class variables are associated with the class and not an instance of the class, i.e., objects. To refer to these elements, you can use the classname and a dot (".") followed by the class method or class variable name. Class methods and class variables are declared with the  static  keyword. Class methods are also called  static methods  and class variables are also called  static variables  or  static fields . An example for the usage of a static field is  println  of the following statement: System.out.println("Hello World") . Hereby  out  is a static field, an object of type  PrintStream  and you call the  println()  method on this object. If you define a static variable, the Java runtime environment associates one class variable for a class no matter how many instances (objects) exist. The static variable can therefore be seen as a global variable. The following code dem