Using Import Statement
Hi,
I understand that in order to access the methods of a different class, I can "import" it. Here is my question though:
Where am I importing it from? I mean, surely if I am importing a class from e.g. the Java.util package, doesn't this package need to be on my computer somewhere?
And also, if I make a .jar file of my application for use on another computer and the files contain import statements, doesn't the other computer need to have these packages on their machine?
I know this seems like a silly question but it's bothering me!
GF
Yep, you can unzip them to see inside. The package name unzips into a directory structure and the Class name is a file. So java.util.HashMap will unzip into a directory java/util and have a file called HashMap.class in it.
You can usually download the source code for these classes also.
Similar Messages
-
When is necessary to use import statements to import classes?
Hi,
I'm sure that it must be necessary to use import statements
in some situations, I'm finding that my code often works fine
without them. For example, some instructions will say it is
necessary to use import.flash.events.MouseEvent; before
MouseEvent.CLICK will work, or that I must import the loader class
before loading an SWF, but I'm finding that I can skip timporting
and it works fine. Could anyone please explain why this it, and
give me some idea of how to knew when to import and when I don't
have to?
Thank you in advance!Importing is only necessary when you create you own classes
or are using a class that Flash doesn't auto import such as the
fscommand package. Any time you create your own custom
package/class, it is also necessary to use import statements unless
you add the folder containing your custom packages to the default
class path. -
Using imported archive in UDF using import statement
Hello Expert,
I am trying to use the class of keystore_api.jar which is impoted in imported archive in my UDF using the import statement
com.sap.engine.interfaces.keystore.KeystoreManager but I am getting the error
package com.sap.engine.interfaces.keystore does not exist
import com.sap.engine.interfaces.keystore.*;
Appreciate your valuable replies.
Kind Regards,
Kubra fatimaHi,
as ur import statement states
com.sap.engine.interfaces.keystore
You need to import the jar in directory hierarchy as above manner.
com[sap[engine[interface[]keystore[JAR]]]]
Regards
Ashu -
What is difference when using import statement with static keyword ?
10. package com.sun.scjp;
11. public class Geodetics {
12. public static final double DIAMETER = 12756.32; // kilometers
13. }
Which two correctly access the DIAMETER member of the Geodetics class? (Choose two.)
A. import com.sun.scjp.Geodetics;
public class TerraCarta {
public double halfway()
{ return Geodetics.DIAMETER/2.0; }
B. import static com.sun.scjp.Geodetics;
public class TerraCarta{
public double halfway() { return DIAMETER/2.0; } }
C. import static com.sun.scjp.Geodetics.*;
public class TerraCarta {
public double halfway() { return DIAMETER/2.0; } }
D. package com.sun.scjp;
public class TerraCarta {
public double halfway() { return DIAMETER/2.0; } }
The correct answer is A,C.I understood how A is the answer ,but can anyone explain me about package import using static keyword.The above example can be used as a reference.
Thanks for your consideration.amtidumpti wrote:
10. package com.sun.scjp;
11. public class Geodetics {
12. public static final double DIAMETER = 12756.32; // kilometers
13. }
Which two correctly access the DIAMETER member of the Geodetics class? (Choose two.)
A. import com.sun.scjp.Geodetics;
public class TerraCarta {
public double halfway()
{ return Geodetics.DIAMETER/2.0; }
B. import static com.sun.scjp.Geodetics;
public class TerraCarta{
public double halfway() { return DIAMETER/2.0; } }
C. import static com.sun.scjp.Geodetics.*;
public class TerraCarta {
public double halfway() { return DIAMETER/2.0; } }
D. package com.sun.scjp;
public class TerraCarta {
public double halfway() { return DIAMETER/2.0; } }
The correct answer is A,C.I understood how A is the answer ,but can anyone explain me about package import using static keyword.The above example can be used as a reference.
Thanks for your consideration.here's a link to a small tutorial:
[http://www.deitel.com/articles/java_tutorials/20060211/index.html] -
Which one better to use - jsp:useBean or import statement
Hi,
I just want to know that which one is better to use jsp:useBean or import statement .
I can instantiate and call method of myclass -
1) by importing the class through import tag in jsp as <%@page import="myclass"%. or
2). by using <jsp:useBean tag....
i have these two option to do the same thing. i know that basically useBean is used to call setter and getter method of bean class and but it can be used to call a normal java file that have some logic .
so what should i used , which one is better and why?
useBean provides scope and object instance so no need to create object by new operator. and with import you have to create an instance .
but which tag should i use in my jsp?
i am confused???ok, means i can use jsp:useBean tag for all my
classes that are not actually bean. so it will be
instantiated at run time and provide efficiency .No. Jsp:useBean is used for java bean components.
>
but when should i use import statement in my jsp and
it happen at translation time so will it create any
type of burden for my code if i import multiple
classes.For non-java beans, you need to import the classes, period.
It's not a burden, it's a necessity. -
Import Statement Different ways to refer ???
Dear Friends,
Can any one help me the difference between the usage of import statement....
1. using import statement
2. using the class directly
that is,
for example ;
1. import java.util.Date;
Date dt = new Date();
2. java.util.Date dt = new java.util.Date();
what's the difference between two reference of Date class...is there any meaning beyond this ?????
Regards,
V.Prasannawhat's the difference between two reference of Date
class...is there any meaning beyond this ?????there is no difference once the code is compiled - the compiler resolves single class names to their fully-qualified names (ie with the package prefix) and that appears in the .class file constant pool
before compilation then my opinion is that having all the imports at the top of the file is good practice because you can see what is being used easily -
Import statement in .java files and .jsp files
Guys I have few mysteries about this import statement.
Please correct me if I am wrong.
1)Suppose if I have a folder c:\SourceFolder it has one.java and two.java files.If I compile one.java then one.class files is created in c:\ClassFolder .Now two.java uses one of the method of one.java,and without using import statement I am able to compile two.java, and two.class file is created in c:\ClassFolder.I assume that the path of one.class was taken from the CLASSPATH environment variable,hence I there was no need for import statement .........am i right ?
BUT... in My.jsp,which is in C:\JspFolder, if I want to use one.class,then I have to specifically import the class I want to use.That is i have to say
<%@page import="one"%>
and since the classpath is C:\ClassFolder ,it finds one.class in the C:\ClassFolder.(Note that My.class is created in a folder different from c:\ClassFolder )
But I am wondering why was there no need for me to import one.class in two.java.
(Note I am using JDeveloper and Apache server)
Please help.
In this case the pSo I am wondering in JSP why it doesn't take the class I am looking for from the classpath .
Help.You have to understand Java scoping rules. In the 1st scenario with the two Java files I am guessing neither one of them begins with a package statement. This means that they belong to the default package. When you compile the compiler uses the classpath to resolve method calls looking in the default package since there was no import statement specified in two.java. JSP is a little different. Now I just read this here in these forums a couple of days ago. Since the classfiles generated from the JSP files are not in the CLASSPATH they are loaded by a different classloader. This is why you have to import all references to all classes in the default package.
-
Import statement in .java files and .jsp files ..correction
Guys I have few mysteries about this import statement.
Please correct me if I am wrong.
1)Suppose if I have a folder c:\SourceFolder it has one.java and two.java files.If I compile one.java then one.class files is created in c:\ClassFolder .Now two.java uses one of the method of one.java,and without using import statement in two.java I am able to compile two.java, and two.class file is created in c:\ClassFolder.I assume that the path of one.class was taken from the CLASSPATH environment variable,hence I there was no need for import statement .........am i right ?
BUT... in My.jsp,which is in C:\JspFolder, if I want to use one.class,then I have to specifically import one.class in My.jsp .That is i have to say
<%@page import="one"%>
in My.jsp.
and since the classpath is C:\ClassFolder ,it finds one.class in the C:\ClassFolder and MyJsp is compiled sucessfully.(Note that My.class is created in a folder different from c:\ClassFolder )
But I am wondering why was there no need for me to import one.class in two.java.
(Note I am using JDeveloper and Apache server)
Please help.It has to do with packages. Most java classes are in a package, the name of which must conform to its place on the filesystem relative to the classpath. By that I mean that if you have com.mystuff.One.java, it must be in a folder com/mystuff where com is located somewhere in the classpath.
What you've done is a little different. I'm assuming a couple of things:
1. you have no package declaration at the top of one.java or two.java
2. you have the current directory "." in your classpath.
Java has the concept of the "default package", which covers classes without a declared package, and in your case is the current directory.
So when you're in c:\sourcefolder and run the compiler, then "."="c:\sourcefolder", and that directory is part of the default package. No import statements are necessary for classes that are in the same package. This is why two.java can call methods in one.java without an import statement.
When you run your jsp, the "current directory" part of your classpath is not c:\sourcefolder, but some other value (probably the directory you start your jsp engine from) You will have to import all non-java-library classes because the jsp itself becomes a java class, with a package that is determined by the jsp engine. -
Executing db2 import statement from java
we have developed an web application using J2EE technology on websphere and DB2 as a backend.
I have to now develop a utility for data uploading from Excel to DB2. we can do it using IMPORT statement now i want to know is it possible to execute IMPORT statement from JSP/ Servelet and using javabeans.Yes, it is possible :-)
-
Problems with IMPORT STATEMENT
Please, can someone assist me? I am using Java Studio Enterprise 8.1 & Netbeans to design a java application and input form. But the wizard keeps on generating repeated class and subclasses even after I have used the import statement. Its making my program looks clumsy.
I just want to use import statement once and start using the variable in the program without repeating the declaration.
For instance, after importing javax.swing.*, I just want to be able to specify in my program (e.g JPanel Coylogo) without the wizard generating another private javax.swing.JPanel Coylogo.
How can I do it. Please help. I will appreciate
You look at the following section of the program to unstand what I mean. Thank you.
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.*;
import java.sql.Date;
* @author */
public class inputForm extends JFrame
private Connection con;
private Statement stmt;
private int k;
ResultSet rs=null;
// Variables declaration - do not modify
private javax.swing.JLabel ClassLabel;
private javax.swing.JPanel Connectivitypanel;
private javax.swing.JPanel Coylogo;
private javax.swing.JCheckBox DataCapable;
private javax.swing.JLabel DateLabel;
// End of variables declaration
* Creates new form inputForm
public inputForm()
//con = new dbConnect();
initComponents();
con = dbconnection();
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents()
Coylogo = new javax.swing.JPanel();
PhoneType = new javax.swing.JPanel();
PhoneIdLabel = new javax.swing.JLabel();
ClassLabel = new javax.swing.JLabel();
phoneid = new javax.swing.JTextField();
DateLabel = new javax.swing.JLabel();
Connectivitypanel.setBorder(javax.swing.BorderFactory.createTitledBorder("CONNECTIVITY"));
pbluetooth.setText("Bluetooth");
pbluetooth.setActionCommand("bluetooth");
pbluetooth.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
pbluetooth.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
pbluetooth.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
pbluetooth.setMargin(new java.awt.Insets(0, 0, 0, 0));
}// </editor-fold>
private void allPhonesTypeActionPerformed(java.awt.event.ActionEvent evt)
}disable add imports or organize imports in your IDE
-
I have a problem of using packages & import statements....
Something like this...
Assume,
i have 2 java classes. (Class1.java & Class2.java)
Class1.java is in --> C:\java\examples\folder1\abc\Class1.java
Class2.java is in --> C:\java\examples\folder2\xyz\Class2.java
I need to access a variable in Class1.java from Class2.java
In that case, how can i use import statement???
"Class1.java" looks like this-->
package abc;
public class Class1{
public static int value = 10;
public static void main(String args[]){
System.out.println("This is class1");
"Class2.java" looks like this-->
package xyz;
import *************************;
public class Class2{
public static void main(String args[]){
System.out.println("This is class2");
System.out.println("Class1 value is"+Class1.value);
}In Class2.java - what should be the import statement???
(Is it import abc; OR any other)
Do i need to set the classpath to both the packages seperately???
To the C:\java\examples\folder1\abc AND C:\java\examples\folder1\xyz
If the above is incorrect, to which folder should i set the classpath?
Pls help!The import statement should be:import abc.Class1;orimport abc.*; // not recommend because of potential name clashesYour classpath should contain both C:\java\examples\folder1 and C:\java\examples\folder2 (that is: any folders containing the top of a package hierarchy).javac -cp C:\java\examples\folder1;C:\java\examples\folder2 <javasourcefiles>
-
Use of wildcard(*) in import statement
I am a bit new in Actionscript 3.0 and I love to learn it.
I saw the following two import statements in an actionscript 3.0 code.
import flash.display.MovieClip;
import flash.display.SimpleButton;
In this case, can I use just juse * sign as in import.flash.display.*; ???
Is this change would cause any problem?
SamiThere are cases when it works and cases when it doesn't, and I don't know if there is a reason. You can try and see if it makes a difference, and if you get error regarding lack of definition you can hopefully determine that you need to explicitly name the class.
Also, it partly depends on where you are implementing the code. Usually when coding in the Actions panel you do not need to import most classes at all. But when you are working with class files you normally have to import any that are not top-level, such as the two you identified. -
Using wildcards in import statement
I typically use wildcards in my import statements. For example:
import javax.swing.*;
Are there any advantages, though, in specifying exactly which classes I am importing?
For example,
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JEditorPane;
import java.swing.JProgressBar;
// etc.
Specigically, is the resulting class file any smaller if I specify exactly which classes to use and does the Java runtime engine load faster if I specify exactly which classes I use in the import statemetents?
Thanks,Import has precisely zero runtime impact. I believe it is used to help locate the specified class at runtime. Take the following 2 simple source files:
import java.util.Vector;
//import java.util.*;
public class VectorTest
public static void main( String[] args )
Vector v = new Vector();
v.add("Item 1");
v.add("Item 2");
System.out.println( v.get(1) );
public class Vector
public boolean add(Object o)
return true;
public Object get(int index)
return "doh!";
}1) Run the code as is and "Item 2" is displayed
2) Recompile the code using the generic import and "doh!" is displayed.
The point is that by fully qualifying the import statement you are sure you are executing the correct class and not just some class that happens to be lying around somewhere in your classpath. -
Import statement using DATA BUFFER
Hi All,
I am using RFC enabled FM using STARTING NEW TASK, We cannot import data from FM back to the program when we use this statement. So I am exporting data into DATA BUFFER in the FM and trying to import data in the main program. Can you please tell me how can I import data from SHARED MEMORY, Below is my code.
call function 'YPMLR_SITEBAL_DETAILS'
starting new task 'ID'
exporting
s_cyl = s_cyl-low
s_lifnr = s_lifnr-low
s_lstyp = s_lstyp-low
tables
s_zlocn = lt_zlocn
gt_zmlr_mld = gt_zmlr_mld
gt_zmlr_lp = gt_zmlr_lp
gt_zmlr_mlp = gt_zmlr_mlp
gt_zmc_loc = gt_zmc_loc.
"IMPORT e_rand_no TO e_rand_no from MEMORY ID 'RAND'.
Here is the export statement used in FM,
EXPORT e_rand_no FROM e_rand_no TO DATA BUFFER XSTR.Hi,
Check this link for Export to database instead of memory..
http://help.sap.com/abapdocu/en/ABAPEXPORT_DATA_CLUSTER_MEDIUM.htm
Import from database instead of memory
http://help.sap.com/abapdocu/en/ABAPIMPORT_MEDIUM.htm -
Hi srinivas bobbala,
Thank you for your response.But I think my quesition was not clear.My ques... is for suppose there is one datafile for importing like.. "datafile_21" Here my intention is it takes file from datafile_21 only but it appears in import statement like datafile_21<<curmon>>.
import database sample.sample data from data_file "c:\\ABC\datafile_21_AUG.txt" using server rules_file datafile on error abort;
In this it takes datafile from datafile_21.But it appears like datafile_21_AUG in import statement.This AUG coming from batch file.
Essmsh c:\\ABC\loadmxl.mxl %curmon%No it is not possible.
I assume the data file "datafile_21" is first renamed to datafile_21_${CurrMth} in the batchscript.
After that this data file *datafile_21_${CurrMth}* is pointed in the import statement.
In the logs you will see this file as datafile_21_Aug.
Maybe you are looking for
-
http://aycu29.webshots.com/image/37828/2005173359849159001_fs.jpg Hi! I am new in java ,How can i build some thing like that in above? Edited by: mjava on Dec 4, 2007 7:26 PM
-
How to associate hardware information into an Oracle Table?
Hi, I am new on Windows 2000 . I am using the following script in order to get the windows 2000 hardware information. I am using 8.1.7 version of ORACLE. I am using a table "Server Hardware"(Server list,Network Adapter,RAM). I would like to ask that
-
ABAP Proxy Class - GET_BUSINESS_SYSTEM_ERROR
I am using ABAP proxy in R/3 to send data to third party system thro' XI. In our development XI it works fine but in our QA-R/3 I am getting error "An error occurred when determining the business system (NO_BUSINESS_SYSTEM)". Does anyone has any idea
-
Execute unix commands from Java
Hi, I have a client application running on windows. This client should connect to a unix server and check for the existence of a file and display the result as "File found/File not found". In order to connect from windows to the unix server, I used t
-
Why does iPhoto open every time I plug in mobile device to charge?
Why does iPhoto open every time I plug in mobile device to charge?