Questions tagged [java-package]

Packages are used in Java in-order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier etc. A Package can be defined as a grouping of related types(classes, interfaces, enumerations and annotations ) providing access protection and name space management

A package allows a developer to group classes (and interfaces) together. These classes will all be related in some way – they might all be to do with a specific application or perform a specific set of tasks. For example, the Java API is full of packages. One of them is the javax.xml package. It and its subpackages contain all the classes in the Java API to do with handling XML.

A package is a grouping of related types providing access protection and name space management. Note that types refers to classes, interfaces, enumerations, and annotation types. Enumerations and annotation types are special kinds of classes and interfaces, respectively, so types are often referred to in this lesson simply as classes and interfaces

  • Every class is part of some package.
  • All classes in a file are part of the same package.
  • You can specify the package using a package declaration:
    package name ;
    as the first (non-comment) line in the file.
  • Multiple files can specify the same package name.
  • If no package is specified, the classes in the file go into a special unnamed package (the same unnamed package for all files).
  • If package name is specified, the file must be in a subdirectory called name (i.e., the directory name must match the package name).
  • You can access public classes in another (named) package using: package-name.class-name You can access the public fields and methods of such classes using:

package-name.class-name.field-or-method-name

You can avoid having to include the package-name using:

import package-name.*;

For more tutorial see here.

96 questions
472
votes
8 answers

javax vs java package

What's the rationale behind the javax package? What goes into java and what into javax? I know a lot of enterprise-y packages are in javax, but so is Swing, the new date and time api (JSR-310) and other J2SE packages.
Jaka Jančar
  • 11,386
  • 9
  • 53
  • 74
12
votes
2 answers

TypeError: 'JavaPackage' object is not callable (spark._jvm)

I'm setting up GeoSpark Python and after installing all the pre-requisites, I'm running the very basic code examples to test it. from pyspark.sql import SparkSession from geo_pyspark.register import GeoSparkRegistrator spark =…
Jessica Chambers
  • 1,246
  • 5
  • 28
  • 56
11
votes
1 answer

Underscore in package name in Java - is it bad?

Sorry for my English I think that sometimes it is necessary. In my opinion search_result_list, location_provider are easier to read than searchresultlist, locationprovider. What does the documentation say?
Leonid Semyonov
  • 473
  • 2
  • 6
  • 18
9
votes
1 answer

Exclude packages from TestNG

Consider the following TestNG configuration which runs all tests in the com.example.functional.* pacakge:
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
7
votes
2 answers

How to expand folder branch in Project Window Intellij IDEA?

Sometimes I close folder/package in Project Tool Window and then I should click every folder in it to open them . How to avoid this and open all folders/packages quickly?
5
votes
1 answer

Why can't I import sun packages?

I downloaded java 9 from https://jdk9.java.net and installed it on my Windows 10 machine. Java 9 has jshell, which I can use to evaluate and learn java programming. Some examples import sun packages, such as sun.audio.* etc. But, in jshell, every…
Germano Carella
  • 473
  • 6
  • 14
4
votes
1 answer

Java - Use of nonpublic class from the current package makes compiling error

I've the following two source files File World.java package planets; public class World { public static void main(String[] args) { Mars.land(); } } File Moon.java package planets; public class Moon { public static void land()…
Vin
  • 701
  • 1
  • 9
  • 30
3
votes
2 answers

module-info not compiled unless package classes are compiled with the same command

I've got a folder called myModule. Inside it, there's another folder called myPackage in which my Main.class exists. Next to myPackage I have my module-info.java which I'd like to compile. First take a look at the folders hierarchy: -- myModule …
Joseph_Marzbani
  • 1,796
  • 4
  • 22
  • 36
3
votes
1 answer

APT: error: attribute 'package' in tag is not a valid Java package name: 'com.mypackage.name_.app'

I have an app which I'm trying to compile with my original package name, very similar to this: com.mypackage.name_.app' The app compiled successfully until now, which I migrated to the newest versions of Gradle and I'm compiling with API 24.…
3
votes
2 answers

JavaFX vs Java Swing usage in Industry

Why the developers refused to start new projects using super easy JavaFX package? Why they continue their projects with traditional Java Swing package? What will be the future of JavaFX? Will it be discontinued? I heard Java Swing is discontinued…
GRTZ
  • 330
  • 6
  • 17
2
votes
1 answer

How to organize code logically into package while preserving encapsulation

This is a conceptual question. I usually organize code in packages logically. E.g.: math operation goes in my.package.math or business logic goes into my.package.business and so on. In these days I have a doubt that I want to share about a…
gixlg
  • 1,193
  • 1
  • 9
  • 21
2
votes
1 answer

Can one Java module export a package whose name is a subpackage of a package from another module?

So I know that, in Java 9 modules (Project Jigsaw), split packages are not allowed. That is, the following modules couldn't both export a package with the same name and also be used at the same time at run time: Module 1 module com.example.foo { …
2
votes
0 answers

What's the easiest way to implement the equivalent of Java's package access in Swift?

I have a set of classes in a subfolder (along with a corresponding group in Xcode) that implement the facade pattern: only one class should be used from outside this folder. How do I implement this access control in Swift? In Java, I'd declare the…
Kartick Vaddadi
  • 4,818
  • 6
  • 39
  • 55
2
votes
1 answer

SharedPreferences managing between different packages

I'm developing one app, with the package com.example1 and I'm doing another module to use as a library with the package com.example2. On my module (com.example2) I'm using this static functions: public static void saveLastDownload(Activity…
Rafael Ruiz Muñoz
  • 5,333
  • 6
  • 46
  • 92
2
votes
0 answers

Using partial package path in Java

I have some classes like a.b.c.v1.Something a.b.c.v2.Something a.b.c.v3.Something There is a place where all of them are visible. Is it possible to arrange that I could refer to them like vX.Something instead of the lengthy a.b.c.vX.Something…
Notinlist
  • 16,144
  • 10
  • 57
  • 99
1
2 3 4 5 6 7