KEMBAR78
Packages,static,this keyword in java | PPTX
Packages in Java
A ppt about java packages , this keyword , static in java
Packages in java
 Package in Java is a mechanism to encapsulate a group of
classes, sub packages and interfaces.
 Packages are used for:
1, Preventing naming conflicts.
2, Making searching/locating and usage of classes, interfaces,
enumerations and annotations easier.
3, Providing controlled access.
4, Packages can be considered as data encapsulation (or data-
hiding).
 Package naming conventions : Packages are named in reverse order
of domain names.
For example, in a college, the recommended convention is
college.tech.cse, college.tech.ee, college.art.history, etc.
 Adding a class to a Package : We can add more classes to an
created package by using package name at the top of the program
and saving it in the package directory.
 Subpackages: Packages that are inside another package are the
subpackages.
 Example :
import java.util.*;
Accessing classes inside a package
 Consider following two statements :
// import the Vector class from util package.
import java.util.vector;
// import all the classes from util package
import java.util.*;
 First Statement is used to import Vector class from util package which
is contained inside java
 Second statement imports all the classes from util package.
Types of packages
Built-in Packages
 These packages consist of a large number of classes which are a part of Java
API.
1, java.lang: Contains language support classes(e.g classed which defines
primitive data types, math operations). This package is automatically
imported.
2, java.io: Contains classed for supporting input / output operations.
3, java.util: Contains utility classes which implement data structures like
Linked List, Dictionary and support ; for Date / Time operations.
4, java.applet: Contains classes for creating Applets.
5, java.awt: Contain classes for implementing the components for graphical
user interfaces (like button , ;menus etc).
6, java.net: Contain classes for supporting networking operations.
User-defined packages
 These are the packages that are defined by the user.
 Steps:
1,First we create a directory myPackage.
2,Then create the MyClass inside the directory with the first statement being
the package names.
code:
package myPackage;
public class MyClass {
public void getNames(String s) {
System.out.println(s);
}
}
 Now we can use the MyClass class in our program
import myPackage.MyClass;
public class PrintName {
public static void main(String args[]) {
// Initializing the String variable with a value
String name = "GeeksforGeeks";
// Creating an instance of class MyClass in the package.
MyClass obj = new MyClass();
obj.getNames(name);
}
}
Note : MyClass.java must be saved inside the myPackage directory since it
is a part of the package.
this keyword
 Here is given the 6 usage of java this keyword.
1, this can be used to refer current class instance variable.
2, this can be used to invoke current class method (implicitly)
3, this() can be used to invoke current class constructor.
4, this can be passed as an argument in the method call.
5, this can be passed as argument in the constructor call.
6, this can be used to return the current class instance from the method.
Static in java
static keyword
 The static keyword in Java is used for memory management
mainly.
 The static keyword belongs to the class than an instance of
the class.
 The static can be:
1, Variable (also known as a class variable)
2, Method (also known as a class method)
3, Block
4, Nested class
Java static variable
 If you declare any variable as static, it is known as a static variable.
 The static variable can be used to refer to the common property of all
objects.
 The static variable gets memory only once in the class area at the time of
class loading.
 Advantages
 It makes your program memory efficient (i.e., it saves memory).
Java static method
 If you apply static keyword with any method, it is known as static
method.
 A static method belongs to the class rather than the object of a class.
 A static method can be invoked without the need for creating an
instance of a class.
 A static method can access static data member and can change the
value of it.
Restrictions
 The static method can not use non static data member or call non-
static method directly.
 this and super cannot be used in static context.
Java static block
 Is used to initialize the static data member.
 It is executed before the main method at the time of classloading.
 Q) Can we execute a program without main() method?
 No, one of the ways was the static block, but it was possible till JDK 1.6.
Since JDK 1.7, it is not possible to execute a java class without the main
method.
Using Static Import
 Static import is a feature introduced in Java programming language ( versions 5
and above ) that allows members ( fields and methods ) defined in a class as
public static to be used in Java code without specifying the class in which the
field is defined.
// Note static keyword after import.
import static java.lang.System.*;
class StaticImportDemo
{
public static void main(String args[])
{
// We don't need to use 'System.out' as imported using static.
out.println("GeeksforGeeks");
}
}
Thank you…
All the contents in this ppt is from geeksforgeeks.org and javatpoint.com.
Ppt made by DoCZ.

Packages,static,this keyword in java

  • 1.
    Packages in Java Appt about java packages , this keyword , static in java
  • 2.
    Packages in java Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces.  Packages are used for: 1, Preventing naming conflicts. 2, Making searching/locating and usage of classes, interfaces, enumerations and annotations easier. 3, Providing controlled access. 4, Packages can be considered as data encapsulation (or data- hiding).
  • 3.
     Package namingconventions : Packages are named in reverse order of domain names. For example, in a college, the recommended convention is college.tech.cse, college.tech.ee, college.art.history, etc.  Adding a class to a Package : We can add more classes to an created package by using package name at the top of the program and saving it in the package directory.  Subpackages: Packages that are inside another package are the subpackages.  Example : import java.util.*;
  • 4.
    Accessing classes insidea package  Consider following two statements : // import the Vector class from util package. import java.util.vector; // import all the classes from util package import java.util.*;  First Statement is used to import Vector class from util package which is contained inside java  Second statement imports all the classes from util package.
  • 5.
  • 6.
    Built-in Packages  Thesepackages consist of a large number of classes which are a part of Java API. 1, java.lang: Contains language support classes(e.g classed which defines primitive data types, math operations). This package is automatically imported. 2, java.io: Contains classed for supporting input / output operations. 3, java.util: Contains utility classes which implement data structures like Linked List, Dictionary and support ; for Date / Time operations. 4, java.applet: Contains classes for creating Applets. 5, java.awt: Contain classes for implementing the components for graphical user interfaces (like button , ;menus etc). 6, java.net: Contain classes for supporting networking operations.
  • 7.
    User-defined packages  Theseare the packages that are defined by the user.  Steps: 1,First we create a directory myPackage. 2,Then create the MyClass inside the directory with the first statement being the package names. code: package myPackage; public class MyClass { public void getNames(String s) { System.out.println(s); } }
  • 8.
     Now wecan use the MyClass class in our program import myPackage.MyClass; public class PrintName { public static void main(String args[]) { // Initializing the String variable with a value String name = "GeeksforGeeks"; // Creating an instance of class MyClass in the package. MyClass obj = new MyClass(); obj.getNames(name); } } Note : MyClass.java must be saved inside the myPackage directory since it is a part of the package.
  • 9.
    this keyword  Hereis given the 6 usage of java this keyword. 1, this can be used to refer current class instance variable. 2, this can be used to invoke current class method (implicitly) 3, this() can be used to invoke current class constructor. 4, this can be passed as an argument in the method call. 5, this can be passed as argument in the constructor call. 6, this can be used to return the current class instance from the method.
  • 10.
  • 11.
    static keyword  Thestatic keyword in Java is used for memory management mainly.  The static keyword belongs to the class than an instance of the class.  The static can be: 1, Variable (also known as a class variable) 2, Method (also known as a class method) 3, Block 4, Nested class
  • 12.
    Java static variable If you declare any variable as static, it is known as a static variable.  The static variable can be used to refer to the common property of all objects.  The static variable gets memory only once in the class area at the time of class loading.  Advantages  It makes your program memory efficient (i.e., it saves memory).
  • 13.
    Java static method If you apply static keyword with any method, it is known as static method.  A static method belongs to the class rather than the object of a class.  A static method can be invoked without the need for creating an instance of a class.  A static method can access static data member and can change the value of it. Restrictions  The static method can not use non static data member or call non- static method directly.  this and super cannot be used in static context.
  • 14.
    Java static block Is used to initialize the static data member.  It is executed before the main method at the time of classloading.  Q) Can we execute a program without main() method?  No, one of the ways was the static block, but it was possible till JDK 1.6. Since JDK 1.7, it is not possible to execute a java class without the main method.
  • 15.
    Using Static Import Static import is a feature introduced in Java programming language ( versions 5 and above ) that allows members ( fields and methods ) defined in a class as public static to be used in Java code without specifying the class in which the field is defined. // Note static keyword after import. import static java.lang.System.*; class StaticImportDemo { public static void main(String args[]) { // We don't need to use 'System.out' as imported using static. out.println("GeeksforGeeks"); } }
  • 16.
    Thank you… All thecontents in this ppt is from geeksforgeeks.org and javatpoint.com. Ppt made by DoCZ.