Solved

basic java question

Posted on 2014-02-04
12
221 Views
Last Modified: 2014-02-18
I how one spaghetti java class. Decided to divide it into smaller classes.

created a directory xxxx where I moved the original java code. I added package 'xxxx' on the top.

package xxxx
class A {

 main()
{

}

}

Then moved couple of static method to a new class without main :

package xxxx
class B {
 
 public static method A();

public static method B();
}

When I am trying to call a method in class A from class B :  B.A()
I am getting an compiler error message that B is not found
0
Comment
Question by:sniger
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39833879
Sounds like you did not create a new instance of class B within Class A.

somewhere within class A... you need to do something like

B newInstanceOfB = new B();

once you have the new instance... then you can refer to the static method.

newInstanceOfB.A();
0
 

Author Comment

by:sniger
ID: 39833899
when I did:
  Styles  style = new Styles()  ( Styles is a class B)


  symbol  : class Styles                              
  location: class xxxx.Excel                        
                        Styles  style = new Styles()

I got a message  cannot find symbol. Class styles is in xxxx directory
0
 
LVL 14

Expert Comment

by:CPColin
ID: 39833902
Ken,

It's a static method; you don't need an instance of the class.

sniger,

If the classes are in two different packages, class B needs to be declared public in order for class A to see it.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:sniger
ID: 39833927
the classes are in the same packages. I declared class B as public
and call static method B.A() and  I am still getting the same error
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39833952
That's because you haven't put in the opening and closing braces for the methods.
0
 
LVL 14

Expert Comment

by:CPColin
ID: 39833956
sniger,

Can you post more of your actual code? It's tough to tell what's going on from the pseudocode you've posted.
0
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39833963
CPColin is right...thanks for the correction... I was thinking that we had to declare an instance of the class because the class wasn't static... but since we are calling a static method... you are right...we should be able to invoke directly using the class name.

@sniger... I created a test program with your conditions... and had no issue calling B.A();
Did you put the code for Class A in A.java and the code for class B in B.java?

In A.java I had this code:
package test;

public class A {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		B.method1();
	}
}

Open in new window

in B.java I had this code...
package test;
class B {
	static void method1()
	{ System.out.println("method1");}
	
	static void method2()
	{ System.out.println("method2");}
}

Open in new window

result of "method1" prints to console.
0
 
LVL 6

Expert Comment

by:Mahesh Bhutkar
ID: 39834929
Assuming you have installed JDK correctly & you are able to compile the class in appropriate directory.
Are you using eclipse IDE? If yes, then it will show you error in eclipse itself even before running your class.
Please check the .class file for both the java classes.
0
 

Author Comment

by:sniger
ID: 39835898
Ken:

I copied your examples and got the same error message. I created directory test
and put those 2 sources when I am trying to compile, I am getting error message:


A.java:7: cannot find symbol  
symbol  : variable B          
location: class test.A        
                B.method1();  
                     
1 error
0
 

Author Comment

by:sniger
ID: 39835905
Ken:

I am using command line...my classpath is as follows:

echo $CLASSPATH                                                              
.:/java/jt400.jar:/java/poi-3.9.jar:/java/poi-ooxml-3.9.jar:/java/poi-ooxml-s
chemas-3.9.jar:/java/xmlbeans-2.3.0.jar:/java/dom4j-1.6.1.jar:/java/javax.mai
l.jar                                                                        
$
0
 
LVL 19

Accepted Solution

by:
Ken Butters earned 500 total points
ID: 39835979
when you compile them... you need to do the following:

>javac A.java B.java

I get the error you got when I just compiled A.java by itself.
0
 
LVL 6

Expert Comment

by:Mahesh Bhutkar
ID: 39838127
javac A.java should work as Class B has been invoked from class A.

Ken, Why do you think javac A.java B.java  command needs to be executed?
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
github account with ecipse 1 50
diffSum example 4 38
check java version using powershell 13 96
how to install java on RHEL image on EC2 4 31
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question