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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Organizing code

Status
Not open for further replies.

idinkin

Programmer
Sep 12, 2002
29
0
0
IL
Hello,

I have two questions:

1. Can I split the implementation and the declaration of a class into two different files? Like it was in C++ using .h and .cpp files.

2. Can I implement two different classes in one .java file?

I'm using Netbeans for developing J2ME applications.
Thanks!
 
Java does not have the concept of 'header' files.

Yes, you can have two different classes within one java file.

Tim
---------------------------
"Your morbid fear of losing,
destroys the light you're using." - Ozzy
 
Is there a way to split the implementation and the declaration of a class though?
 
Not like you mean.

You can have typed interfaces which define 'contracts' which implementing classes must adhere to. Any class can implement any given interface as long as it provides the methods defined in the interface... and a class can implement more than one interface.

Tim
---------------------------
"Your morbid fear of losing,
destroys the light you're using." - Ozzy
 
Can you please explain the way of doing this, give me an example?
 
Simple example :

Code:
public interface MyInterface {
  public void doSomething();
}

Code:
public class MyInterfaceImpl implements MyInterface {
  public void doSomething() {
    // do something
  }
}

Its a bit like prototypes and implementations in C.

Read more :

--------------------------------------------------
Free Database Connection Pooling Software
 
public interface" is often given in the form of the javadoc, a java interface generally serves a different role (as a method of inheritence).

A javadoc comment take the form
Code:
/** This function does something useful
 * [green]param[/green] [red]f[/red] that it uses for doing useful stuff
 * [green]return[/green] the useful result
 * [green]throws[/green] [red]IOException[/red] when something bad happens
 **/
int do_stuff(int f) throws IOException

The java doc acts like a header in that it tells the USER what the user can expect. The output of javadoc is generally a pretty HTML page that looks like the api pages on the java site at . This is the public contract for users. java interfaces are contracts with other modules...

You can include multipule classes in a java file, but it can't be public. Only one top level public class is allowed, but other nested public classes are allowed.

[plug=shameless]
[/plug]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top