Solved

basic java question

Posted on 2014-02-04
12
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java string replace 11 80
maven disable workspace resolution 1 79
What is the use of Forwarding Class in java 1 54
maximize the sum of fractions 33 76
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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

710 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