Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

basic java question

Posted on 2014-02-04
12
Medium Priority
?
231 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 17

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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

916 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