How to static import a class from default package? [duplicate] - java

This question already has answers here:
How to access java-classes in the default-package?
(5 answers)
Closed 5 years ago.
I have 2 classes: class A and class B, both classes are packageless (in default package). I want to import and use A's static variable into B. How do I do that so that it compiles?
The following is not compiling:
A.java
public class A {
public static int x = 10;
}
B.java
import static A.x;
public class B {
public static void main(String[] args) {
System.out.println(x);
}
}
Compiler output:
B.java:1: error: static import only from classes and interfaces

This is impossible with java, you have to package them in a unique or different package.
Or you can use :
System.out.println(A.x);
You can read more in java doc about Import Declarations

Related

How to import functions from different files in Java [duplicate]

This question already has answers here:
Calling static method from another java class
(3 answers)
Closed 1 year ago.
In java, I know I can use methods from other class by creating that class in my main myClass newClass = new myClass() and then use methods by doing myClass.myMethod(), where myMethod() is defined in myClass.java, but what if I want a file named utils.java that contains a bunch of useful methods, I just want to use a function that there's in this file, is this possible? How can I import and use the functions from that file?
You do not need to "import" the functions, you would need just to create a class that keeps all your needed functions, and import that class.
I am supposing you use an IDE, you could go to your IDE and create a simple class.
e.g:
public class Utils
{
public static int doSmth(/* Your parameters here */)
{
// Your code here
}
public static void yetAnotherFunction(/* Your parameters here */)
{
// Your code here
}
}
The most important keyword here is static, to understand it I suggest you check my answer here: when to decide use static functions at java
Then you import this class to any of your other classes and call the static functions, without creating an Object.
import package.Utils;
public class MainClass
{
public static void main(String[] args)
{
Utils.doSmth();
}
}
You can add at the beginning:
import
like-
import utils;
Assuming it is in the same package as the current class
Else it should be:
import <package name>.<class name>
Yes, you can import methods but there are caveats. The methods are defined on a class. The methods are defined as static. The import employs the keyword static. Keep in mind that importing methods directly can create confusion and complicate debugging. Consider importing the class and invoke the method from the class. When/whether defining a utility class makes sense and how to implement them are separate discussions.
package some.path;
public class MyUtils {
public static int add(int x, int y) { return x + y; }
}
To import the method
package some.other.path;
import static some.path.MyUtils.add; // note keyword static here
public class MyClass {
private int a, b;
public int sum() { return add(a,b); }
}
Or, import the class and use the method statically
package some.other.path;
import some.path.MyUtils;
public class MyClass {
private int a,b;
public int sum() { return MyUtils.add(a,b); }
}

Java access specifier issue [duplicate]

This question already has answers here:
What is the difference between public, protected, package-private and private in Java?
(30 answers)
Closed 5 years ago.
I am confused with access specifiers in Java.. I have a code with 2 different packages in Java.. But it displays an error every time I run it.. Here's is the code for the class which is calling the class using import from another package..
package p2;
import p1.Testing;
class Pqr extends Testing{ // extending the class
void hey(){
System.out.println("Something");
}
}
class Xyz{
public static void main(String args[]){
Pqr t1 = new Pqr(); // Class from another package.
System.out.println(t1.find("Mississippi","p"));
t1.hey();
}
}
Code for class which is being subclassed from package p1..
package p1;
class Testing{
protected static boolean find(String a,String b){ // Protected specifier
boolean ans = false;
for(int i=0;i<a.length();i++){
String m = a.charAt(i) + "";
if( m.equals(b)){
ans = true;
}
}
return ans;
}
public static void main(String args[]){
// Main Class
}
}
But when I run the code i get an error "Testing is not public in p1; cannot be accessed from outside package"..
I learned in this thread that we can use protected method between different packages but by extending it by another class.. In Java, difference between package private, public, protected, and private
Thanks in advance.
You don't have an access to the top-level class (in your case it's Testing), so you can't have an access to its members, no matter what are its access modifiers. You need to make Testing public to make its protected members visible from outside of its package to classes extending Testing. You can read more about it in Java tutorial. Here is a part about access modifier of Testing class:
A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package
And the part about your find() method:
The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.
But to make use of protected members of class contained in another package, the class itself containing it need to be visible in another packages (it needs to be public).
Take a look at this Java tutorial for more informations about access modifiers:
https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Static method overriding in Java [duplicate]

This question already has answers here:
Why doesn't Java allow overriding of static methods?
(22 answers)
Is it possible to override a static method in derived class?
(6 answers)
Closed 5 years ago.
class B extends A {
static public void printMe(){
System.out.println("in static B");
}
}
class A{
static public void printMe(){
System.out.println("In static A");
}
}
public static void main(String[] args) {
A a = new B();
a.printMe();
}
Why is the output "In static A" ?
Static members bind to type rather than implemented type. Hence you see the methods executing from class A.
And static members are not to be ovverriden and they share same copy regardless of instance state.
If you need methods to be ovveriden, do not use them as static members.
Static member or method belongs to class level instead of specific instance.
A static class will have only 1 instance even if you create multiple instance or do not create instance.
In your case, since you have created instance of class A, method inside class A is implemented.
Another way to get a clear concise for the problem scenario is try running the code mentioned below:
public static void main(String[] args) {
A.printMe();
}
You will get clear idea.

Accessing protected members [duplicate]

This question already has answers here:
Protected member access from different packages in java - a curiosity [duplicate]
(9 answers)
Closed 7 years ago.
I am very confused with this . can any one clear me why it was not allowing the code that i have placed n comments
package pack1;
public class A {
protected void m1() {
System.out.println("This is very imp point");
}
package pack2;
import pack1.A;
class B extends A {
public static void main(String arg[]) {
// A a1 = new A();
//a1.m1();
B b1 = new B();
b1.m1();
//A a2 = new B();
//a2.m1(); }
}
}
Method m1 is protected, so it's accessible across packages to child classes.
Therefore, instances of B will be able to invoke or #Override m1.
Not so main static methods, even if pertaining to class B: the scope is different.
You can either make m1 public in A, or invoke it within your instance of B (e.g. in the constructor, etc.).
You can also override A's m1 in B and give it less access restrictions, thus making it public in this instance: then you could access it on an instance of B from your main method as you're trying to do.
You can access the protected members declared in A within B, but only for instances of B or subclasses of B. Check out this answer

Java weird static import [duplicate]

This question already has an answer here:
static member in Java
(1 answer)
Closed 8 years ago.
package pack1;
public class A {
public static int i = 10;
public static void test() {
System.out.println("done");
}
}
This is separate .class file:
package pack2;
import pack1.A.*;
public class Manager0 {
public static void main(String args [] ) {
System.out.println(A.i);
A.test();
}
}
When I run Manager0 class it shows error because I used import pack1.A.*; instead of import pack1.A;, but why doesn't import pack1.A.*; work? I mean doesn't import pack1.A.*; mean import everything in class A?
You're looking for static imports. That should be:
import static pack1.A.*;
.. and you should really avoid it, specially using like that. If you want a specific member, import it specifically by using it's name.
It failed without static because you can't import members of a class non-statically. You can only use so called Import-on-demand, to import classes from a particular packages, or static import-on-demand to import static members of a class.

Categories

Resources