Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to call method from another class

Status
Not open for further replies.

ketandba

Programmer
Nov 15, 2004
38
US
i am newbie in java...

keep in mind that every method is non-static.
all variables are also non-static.
here is my three classes....

My Aim --- to call toDom() from InvoiceNotes class to
addDocument() of InvoiceNotesController

1)
public class InvoiceNotesController {

public static void main(String args[]){
InvoiceNotesController inc = new InvoiceNotesController();
inc.init("a.xml");

}

private void init(String inFile){
try{
InvoiceNotesHandler handler = new InvoiceNotesHandler();
parseXmlFile(inFile, handler, false);

addDocument("aXmlId");

}catch (Exception e) {
e.printStackTrace();
}
}

public void parseXmlFile(String filename, DefaultHandler handler, boolean validating){
----------> Some parse code here <-----------------------------
}
public void addDocument(String xmlFileId) throws Exception {
Collection col = null;
String id;
************************************************************************************************************************************
How i call toDom() method from InvoiceNotes Class without creating new Instance of InvoiceNotes.....
Also all methods are instance Method and all variables are instance variables....Not used static any way ----
***********************************************************************************************************************************
try {
col = XmlDbManager.getCollectionInstance("aaa",4444,"ots","username","xxxx");

=========> Node finaldoc = (Node)toDom(); <=========

XMLResource document = (XMLResource)col.createResource(xmlFileId,"XMLResource");
document.setContentAsDOM(finaldoc);

col.storeResource(document);
String target = col.createId();

System.out.println("Document inserted with Id - " + xmlFileId);
}catch(XMLDBException e) {
System.err.println("XML:DB Exception occured " + e.errorCode);
e.printStackTrace();
}catch(CouldNotConnectToXmlDbException ce) {
System.err.println("Could Not connect to XmlDb");
ce.printStackTrace();
}finally {
if (col != null) {
col.close();
}
}
}

2) Here InvoiceNotesHandler class.......

public class InvoiceNotesHandler extends DefaultHandler {

private String currentQname = null;
private StringBuffer thisText = new StringBuffer();
private String clientName = null;
private String dateString ;
private String descriptionString ;
private String minutesString ;
private float hours ;
private Date date ;
************ ***********
InvoiceNotes in = new InvoiceNotes();
*********** **********
public void startDocument() {

}

public void endDocument() {

}

public void startElement(String uri,
String localname,
String qname,
Attributes attributes)
throws SAXException {

if(qname.equals("CLIENT")) {
clientName = attributes.getValue(0);
in.chkContainsKey(clientName);
}
currentQname = qname;
}

public void endElement(String uri,
String localname,
String qname)
throws SAXException {


if(qname.equals("DATE")){
dateString = thisText.toString();
SimpleDateFormat sdf = new SimpleDateFormat("MMddyy");
try {
date = sdf.parse(dateString);
}catch(Exception e){
e.printStackTrace ();
}
}else if(qname.equals("DESCRIPTION")){
descriptionString = thisText.toString();
}else if(qname.equals("MINUTES")){
minutesString = thisText.toString();
hours = new Float(minutesString).floatValue();
}else if(qname.equals("REC")){
Record record = new Record(date, descriptionString, hours);
in.addRecord(record);
}else if(qname.equals("CLIENT")){
in.putMap(clientName);
}
}


}
3) here invoicenotes class.....

public class InvoiceNotes {

SortedLinkedList linkedRecordList = null;
TreeMap clientHistory = new TreeMap( new StringComparatorIgnoreCase( ) );
Document doc = null; // output DOM document

public void chkContainsKey(String clientName){
linkedRecordList = new SortedLinkedList();
if (clientHistory.containsKey(clientName)){
//--Here i am creating new SortedLinkedList and
// add each record to linkedRecordList

linkedRecordList = (SortedLinkedList)clientHistory.get(clientName);
}
}

public void addRecord(Record record){
linkedRecordList.add(record);
}

public void putMap(String clientName){
clientHistory.put(clientName,linkedRecordList);
}

public Document toDom(){
-----------> code for retrieve from treemap and create DOM document <-----------------------


doc.appendChild(documentElement);
return doc;
}
}

***** end ****
pl. give me the solution...
Thanks in advanced..
your help is appriciated.....

ketan


 
Have we not been through this before ?!! thread269-963630


--------------------------------------------------
Free Database Connection Pooling Software
 
hi,
sedj,
sorry we have not solve this problem in thread269-963630.
Here i have change my code...i got the result with static variables/method but my project manager told me that you have to not used static variables/methods...

pl. help me out...

Thanks
ketan
 
How i call toDom() method from InvoiceNotes Class without creating new Instance of InvoiceNotes.....

Short answer: You can't.

Longer answer: One of the points of object-oriented programming is that you can have different instances of a class (each containing their own data), that you create using the new operator. If you (for whatever reason) decide that a method should work without requiring an instance you create a static method. This is where your manager said no, and he did it to prevent two common scenarios:

1. Your code ends up being procedural in nature, rather than O-O. This leads to maintenance problems down the road.

2. You end up with classes which are "kitchen sink" code -- they contain a little bit of everything, none of which really belong together. After which you're playing the classic "Sesame Street" song: <i>One of these things isn't like the others</i>.

I would invest in a couple of books to help you learn the O-O paradigm, one of which should be Bruce Eckel's "Thinking in Java" (available for download online as a .pdf)

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top