You are on page 1of 25

Java 1.

Accepting Input, Packages, Access


Modifiers
Input in Java
• There are many ways of accepting input in
Java, but the prominent method is using
Scanner class.
• Accepting input in Java is not as direct as in
C/C++.
Example for accepting input in Java
import java.util.Scanner;

class GetInputFromUser
{
public static void main(String args[])
{
int a;
float b;
String s;

Scanner sc = new Scanner(System.in);

System.out.println("Enter an integer");
a = sc.nextInt();
System.out.println("You entered integer "+a);
System.out.println("Enter a float");
b = sc.nextFloat();
System.out.println("You entered float "+b);

System.out.println("Enter a string");
s = sc.nextLine();
System.out.println("You entered string "+s);
sc.close();
}
}
If the input given to the program is:
5
11.67
Hello
Then determine the output:
Points to note:
• import java.util.Scanner;
• Creating an object of Scanner class
• Use methods of Scanner object to accept input.
• Close the Scanner object you created.
Important methods of Scanner class
Example Program of Scanner class
next() vs nextLine()
• Both of them are functions of Scanner class.
• Both of them are used for scanning a string.
• next() scans till it finds a space
• nextLine() scans till it finds a “\n” newline
character.
Packages
• A package in Java is similar to a namespace in
C++.
• It is like a folder, where you store classes,
interfaces and sub-packages.
• There are two types of Packages:
• Built-in Packages like java, lang, util, etc.
• User-defined packages.
Advantages of Packages
• It is used to categorize the classes and
interfaces for easy maintenance.
• It provides access protection.
• It removes naming collision.
Package Hierarchy in Java
import keyword
• To use any functionality of a predefined
class/interface, you have to import the
package containing it.
• import statement should be the first line of
any java program.
Access Modifiers in Java
• There are four access modifiers in Java:
1. private
2. protected
3. public
4. default (no keyword)
private
• private members are accessible only within a
class.
• A class cannot be private or protected unless it
is a nested class.
• If you define a private constructor, you cannot
create objects from outside of the class.
Sample Program
Sample Program
default
• If you don’t use any access modifier, it is
treated as default.
• The default is accessible only within the same
package.
Sample program
protected
• It is accessible within the package and to
derived classes.
• This access modifier can be applied on data
member, method and constructor.
• It cannot be applied to a class.
Sample program
public
• It is accessible everywhere.
• It can be applied to classes as well.
Sample Program
Conclusion
static keyword
• It is used for memory management.
• It can be used with variables, methods,
unnamed blocks and nested classes.
• static members belong to the class rather than
its individual objects.
static keyword can be applied to:
1. variable
2. method
3. block
4. nested class

You might also like