Mr ramu dayinaboyina
package complex;
public class c //here remove the public
because we are saving this total as cominside.java
but public is allowed if save this file
separately like c.java
{
int r,i;
public c(){}
public c(int r1,int i1)
{r=r1;
i=i1;}
public void add(c c1,c c2)
{r=c1.r+c2.r;
i=c1.i+c2.i;}
public void view()
{System.out.println(r+"+i"+i);}}
//here now need to write the import
statement
class cominside
{
public static void main(String args[])
{
c a=new c(3,2);
c b=new c(1,2);
a.view();
b.view();
c cr=new c();
cr.add(a,b);
cr.view();
}
}
Case-1 package class and main method class in a single file
sineraio like below
In the above I am putting the main method class with the package class file itself
in the above code I am going to store the entire file with name
cominside.java instead of separately saving c.java and cominside.java we know
that if we have more than
one class in a single java file with what
name we are going to save that class
only allowed to declare as public
we are saving with the main method
class this class only allowed to declare as public but not the class name with c
E:\jit\complex>dir Volume in drive E is New Volume Volume Serial Number is 003D-F678
Directory of E:\jit\complex
21-Mar-12 12:32 AM 822 c.class
21-Mar-12 12:54 AM 406 cominside.java
2 File(s) 1,228 bytes
E:\jit\complex>javac
cominside.java
cominside.java:2: class c is public,
should be declared in a file named c.java
public class c
^ 1 error
E:\jit\complex>javac cominside.java
E:\jit\complex>java cominside
//execuation is not allowed here but it will allows the compilation to
get it compiled u go back to its parent directory from there u call package
name .classname otherwise u get the below exception
Exception in thread "main"
java.lang.NoClassDefFoundError:cominside
E:\jit\complex>cd..
E:\jit>java complex.cominside //you must get back to the package parent
directory then interpret the main class execution
3+i2
1+i2
4+i4
Case-2 package class is in separate folder and its
calling class in the just above one level I.e. in the parent directory the
scenario lie the below
U define c.java file with package statement
package complex u can compile this 2
ways one is
If u saved the above file e:\jit>
in this location
Then
give the below command e:\jit>javac
–d . c.java
Then in the e:\jit> another folder
is created with name complex folder and it contains c.class file
After compilation u immediately remove the c.java
source file from the location e:\jit>
and in this location implement the main method class
with the import statement
The other one is
First create manually folder with the
package name what u wrote in the package class
then compile inside the package folder itself like below
e:\jit>md complex
e:\jit>cd complex
e:\jit\complex>javac c.java
Here only compilation allowed here but
not the interpretation
Note c.java u are allowed put the
class name as public and the
main method class allowed to put once
again public u must save with respective class names as their filenames u must use the explicit import statement in the main method class
For the execution come out from the
package folder cd..
e:\jit>javac cominside.java directly u are allowed to execute
e:\jit>java cominside
Total package
implementation screen shot :-
C:\Users\ramu>set
classpath=%classpath%;e:\jit;.
I:\>dir s*.java
Directory of I:\
21-Mar-12 09:15 PM 146 samepack.java
1 File(s) 146 bytes
I:\>javac samepack.java
I:\>java samepack
I AM INSIDE THE BASE CLASS CONSTRUCTOR
కామెంట్లు లేవు:
కామెంట్ను పోస్ట్ చేయండి