SlideShare a Scribd company logo
1 of 16
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.

More Related Content

What's hot (20)

Java packages
Java packagesJava packages
Java packages
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Interface
InterfaceInterface
Interface
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
5 collection framework
5 collection framework5 collection framework
5 collection framework
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java packages
Java packagesJava packages
Java packages
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Java package
Java packageJava package
Java package
 
Packages
PackagesPackages
Packages
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Packages in java
Packages in javaPackages in java
Packages in java
 

Similar to Packages,static,this keyword in java

Similar to Packages,static,this keyword in java (20)

Packages access protection, importing packages
Packages   access protection, importing packagesPackages   access protection, importing packages
Packages access protection, importing packages
 
Java packags
Java packagsJava packags
Java packags
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
Packages in java
Packages in javaPackages in java
Packages in java
 
packages unit 5 .ppt
packages  unit 5 .pptpackages  unit 5 .ppt
packages unit 5 .ppt
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
 
Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Package.pptx
Package.pptxPackage.pptx
Package.pptx
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core Parctical
 
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptxTHE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
 
Z blue interfaces and packages (37129912)
Z blue   interfaces and  packages (37129912)Z blue   interfaces and  packages (37129912)
Z blue interfaces and packages (37129912)
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
 

Recently uploaded

AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 

Recently uploaded (20)

AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 

Packages,static,this keyword in java

  • 1. Packages in Java A ppt 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 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.*;
  • 4. 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.
  • 6. 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.
  • 7. 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); } }
  • 8.  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.
  • 9. 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.
  • 11. 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
  • 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 the contents in this ppt is from geeksforgeeks.org and javatpoint.com. Ppt made by DoCZ.