sometimes we can from ClassA do :
import com.software.ClassB;
classbObject = new ClassB ;
ClassB.getMethodX();
and we can also from ClassA make directly
import com.software.ClassB;
classbObject = new ClassB ;
classbObject.getMethodX();
what is the diffrence between these 2 cases (calling directly the class or the object)? is there one recommended?
thanks,
If you mean the same thing as I mean: no, there is no functional difference between ClassB.getMethodX() and classbObject.getMethodX() since the getMethodX is static. Only your compiler may give you a warning that classbObject.getMethodX() is discouraged because of the static method call from a non-static context.
You mean why this :
ClassB.getMethodX()
is different from this?
classbObject.getMethodX()
If so, then the second is somehow wrong. I mean it still works, but it makes no sense. The method is declared as static, which "belongs" to the class. You have one static method for a class, not matter how many instances. So, every static method should be called in a static way:
ClassB.getMethodX()
The only way you can do ClassB classbObject without an import is if they are in the same package. If you are asking, should you use an import the answer is, use an import if it is required because the class is not in the same package.
I don't see a static import in your question so not sure how that relates to your question.
Related
I hava a StdDraw.java under the same folder of my working file, and picture() is a method within StdDraw.java.
However, I failed adding this line to import the method, suggesting by
package StdDraw does not exist
import StdDraw.picture
How could I possibly do that? Using package? Setting path? Or any modifications? I came from python and find it a little bit weird.
You can't import non-static methods (only classes and static members), and you don't have to!
If both your classes live in the default package then you should be able to do the following without any import statements :
myStdDrawObject.picture(); // if picture is non-static
or
StdDraw.picture(); // if picture is static
Note also, that you can't use static imports on classes that live in the default package.
If you are importing into the class which is there in same package then we no need to use any import.
if you want import mthods into the class use like below. You no need to put method name at the time of the import.
import packagename.StdDraw;
After importing your class, all non static methods of the class are available into the imported class.
when should you use static import? Only use it when you'd otherwise be
tempted to declare local copies of constants, or to abuse inheritance
(the Constant Interface Antipattern). In other words, use it when you
require frequent access to static members from one or two classes. If
you overuse the static import feature, it can make your program
unreadable and unmaintainable, polluting its namespace with all the
static members you import. Readers of your code (including you, a few
months after you wrote it) will not know which class a static member
comes from. Importing all of the static members from a class can be
particularly harmful to readability; if you need only one or two
members, import them individually. Used appropriately, static import
can make your program more readable, by removing the boilerplate of
repetition of class
names.
Read more about static import:
https://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html
When importing the package you do not need to import non static methods. You can read on Java - Packages here. It is easily explained and I found it useful when learning the same concept.
Even if you do not include import for the class which is present in the same folder, then also you can create object and call method of that class and also static methods.
You can create object and call the non-static methods.
StdDraw drawObj = new StdDraw();
drawObj.picture(); // if picture is non-static method
For static method, you can call it using class name only.
StdDraw.picture(); // if picture is static method.
What I recommend is to read up on packages and how code is organized in java. It is in someway similar to python, where a directory structure is used, but more to it in java. Maybe this will help
Java Tutorial- Packages
I've seen in some projects people importing directly a static method into an Activity or a class instead of the whole class.
My question is, knowing that a static method can be called several times (for being more accurate, 5 or more times) in the same class, is it more efficient to import the static method or it is better to call it through its class?
Your question (according to me) does seems to address the same point.
The question is if:
is calling ClassName.staticMethod() directly
or this
import static ClassName.staticMethod;
//rest of the stuff
staticMethod();
In both cases, the methods are loaded as a singleton whenever the class is called first.If you import the static method,then thats the first time or else when you use the class's method.
So it wont make a difference because the JVM/DVM (not sure about ART) already has the data required.
IF however your question is regarding what modifiers to use,then this advocates static.
But as mentioned,involving static methods directly is just messy.
So now its more of a personal choice.
Read as:Do not import static methods directly unless you have a very specific reason to do so.
There is no difference in performance between static imports and import the class.
However, import the class and use the class name to call static methods is considered a better practice, because the code is more easy to read. With static imports it could be a bit confusing which methods are non-static methods of the class and which methods are static methods of other classes.
I wrote a method public static String timestamp() in a class called Utilities, which I want to call in every System.out.println().
Can you tell me how I can call this method in a different package? For example when I have package XY, with the class XY in it, then I can't call the timestamp() method, which is in the root src folder.
You need to use the whole class name, so as your class is called "Utilities":
Utilities.timestamp();
Will call your method. Remember to import your "Utilities" class at the top of your calling class.
Alternatively, if you want to just call:
timestamp();
You can statically import the class:
import static com.foo.Utilities
You need to import the package containing the class containing the timestamp() method. If you haven't declared a package for the class, you should do it now. After importing you can just use Utilities.timestamp() to call it.
If you're unfamiliar with packages in general, you should see the Oracle tutorials: http://docs.oracle.com/javase/tutorial/java/package/
So, more often than not, utility classes are declared as static. I'll give you an example.
Example
public class Utilities
{
public static String timestamp()
{
// Return timestamp.
}
}
Then in your XY class, you need to import your Utilities class, and you can access it as follows.
Utilities.timestamp();
You can call it using:
Utilities.timestamp();
from any class. Remember to import the Utilities class
It's static so you can just call Utilities.timestamp();
I have two java classes (.java files). I want to be able to create new instances of an object in one class that were defined in the other class, without referencing the class name each time. In C# there are #using and includes, but I am only able to use import <pre-compiled code> in java. Is there a way to do this:
import TestClass;
and then simply call a function inside it without using
TestClass.TestFunction()
every time? I simply need to have all of my functions and objects to be separate from my Main class.
Assuming TestFunction is a static method in TestClass, you can use a static import:
import static TestClass.TestFunction;
// or
import static TestClass.*;
and then call it without using the class qualifier:
TestFunction(...);
Note this can lead to confusing/hard-to-read code – use static imports sparingly.
If you're using netbeans then you won't be finding any issue during the import of any java classes. And yeah that's true you can have only "pre-compiled classes" as import statement. If you are running on notepad then you need to compile your independent classes first and then your dependent classes. And if you use Netbeans or Eclipse or any other IDE then you do not have to worry they will manage by themselves, you just have to use proper package and class names
You can have two types of imports
import package1.Class1;
import static package1.Class2;
With the first one you can create object of Class1 (or any other class if present in Class1) and invoke the methods
With the second one you can directly call the static methods of Class2 without referencing it with classname
Updated
See tutorials on packages in JAVA
I'm mostly a c/C++/objective-C programmer, presently working in Java on an android application. My question is simple: I want a utility function, preferably not associated with any class that I can invoke from anywhere in my project (#include of some sort necessary?).
I know I can make a public static function of a class and invoke it as Class.someFunction();. I would like to just have someFunction(); I'm not sure if this is possible in java, or what the syntax for it is.
You can achieve the same "effect" by using a static import, by adding the following import in each file that you want to use it in:
import static x.y.Class.someFunction; // or x.y.Class.*;
...
// some code somewhere in the same file
someFunction();
However, all methods in Java must be part of a class. Static imports just let you pretend (briefly) otherwise.
P.S. This also works for static fields.
You could use a static import:
import static com.example.MyUtilityClass.*; // makes all class methods available
// or
import static com.example.MyUtilityClass.myMethod; // makes specified method available
You don't see this used very often because, if overused, it causes harder-to-debug code (see the last paragraph at the link above).
Here's a related question about when it's advisable to use this.
Also, following the programming best practices, You should define all such common, frequently used functionality in some utility class where you can define your functions or fields(probably constants- i.e. static and final attributes) that is going to be used/called at different places within the API.
Although, you still need to import the Utility class.
Else define such functionality in the top most parent class in your API hierarchy structure, that way you even don't have to import the class.
Hope this helps.
thanks....!!!
Yeap import static..
For instance:
import static java.lang.Math.max; // Allowing to use max method anywhere in the source
class SomeClass {
int m = max( 1, 2 );// m now is 2 due t Math.max( int, int )
}