0

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.

tonyabracadabra
  • 319
  • 2
  • 10

5 Answers5

4

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.

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • how does we can import static methods? –  Jun 09 '15 at 11:10
  • Example `import static java.lang.Math.sqrt;` – aioobe Jun 09 '15 at 11:12
  • So what exactly should I do now to use that method? – tonyabracadabra Jun 09 '15 at 11:12
  • @tonyabracadabra, first let me know, is `picture` a static method (does it have the `static` modifier)? – aioobe Jun 09 '15 at 11:13
  • @tonyabracadabra don't practise with default packages. see http://stackoverflow.com/questions/7849421/is-the-use-of-javas-default-package-a-bad-practice – venkat Jun 09 '15 at 11:14
  • @tonyabracadabra If you are in same package no import is needed just call it by creating object of class in which method is define. –  Jun 09 '15 at 11:14
  • @aioobe picture() is a static method – tonyabracadabra Jun 09 '15 at 11:16
  • @tonyabracadabra, then you should be able to use `StdDraw.picture()` without any import statement. Try this, and if it doesn't work, let me know what the compilation error is. (I'm assuming now that you haven't organized your code in packages, let me know if any file has a `package` declaration.) – aioobe Jun 09 '15 at 11:21
1

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

1

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.
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
1

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.

Zack
  • 1,527
  • 2
  • 20
  • 32
0

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

vijay
  • 444
  • 1
  • 3
  • 7