Home » Posts tagged "Learn Fundamentals of Java" (Page 2)

Learn Fundamentals of Java Programming “User Defined Methods in Java Programming ” Lesson 23

Data Input The programs we have written up to now haven’t been interactive. A program is interactive if it is able to take input from the user and respond accordingly. Let us write a program to take user input. To take user input we use the prebuilt Scanner class. This class is available in the java.util package. First we import this class, import java.util.Scanner; Now we create an object of the...
Continue reading »

Learn Fundamentals of Java Programming “Java Libraries in Java Programming ” Lesson 22

Java Libraries The power of Java comes from the hundreds of Java classes that are already prebuilt and can be used in your programs. To use a prebuilt class and associated methods in those class, all you have to do is to use the keyword import to import the class from the package in which it is contained into your space. The import statements must appear before any class definitions in...
Continue reading »

Learn Fundamentals of Java Programming “Getter and Setter Methods in Java Programming ” Lesson 21

Getter and Setter Methods Private data members of a class cannot be accessed outside the class however, you can give controlled access to data members outside the class through getter and setter methods. A getter method returns the value of a data member. For example we could define a getter method in the Book class for the price data member as given below: double getPrice() { return price; } Similarly, we...
Continue reading »

Learn Fundamentals of Java Programming “Access Modifiers in Java Programming ” Lesson 20

Access Modifiers In the previous section, we allowed access to the data members of the class from outside the class. We created Book objects in the BookTest class and were able to set the object’s data members. However, it is generally not good programming practice to allow data members to be accessed outside the class. By allowing objects to change their data members arbitrarily, we lose control over the values being...
Continue reading »

Learn Fundamentals of Java Programming “Constructors in Java Programming ” Lesson 19

Constructors A data member that is declared but not initialized before using, is assigned a default value by the compiler usually either zero or null. However, it is good programming practice to always initialize variables before using them. A special method member called the constructor method is used to initialize the data members of the class (or any other initialization is to be done at time of object creation). The constructor...
Continue reading »

Learn Fundamentals of Java Programming “Object Oriented Programming in Java Programming ” Lesson 17

Object Oriented Programming By now you know quite a bit of Java programming. Now we begin to look at Java’s most fundamental feature – Classes and Objects. Java is an Object Oriented Programming (OOP) language. In an OOP language, a program is collection of objects that interact with other objects to solve a problem. Each object is an instance of a class. Imagine you are creating a database application for a...
Continue reading »

Learn Fundamentals of Java Programming “User Defined Methods in Java Programming ” Lesson 16

User Defined Methods A method in Java is a block of statements grouped together to perform a specific task. A method has a name, a return type, an optional list of parameters, and a body. The structure of a Java method is as below: return_type method_name(list of parameters separated by commas) { statements return statement } The method name is a word followed by round brackets. The parameter list placed within...
Continue reading »