• Post Reply Bookmark Topic Watch Topic
  • New Topic

structure of a java program  RSS feed

 
lalit khera
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends, i am new to java technology.
just reading head first java -kitthe sierra.
please tell me what is the structure of a java program.
like do we have multiple classes in one program.
please help
 
Michael Miller Jr
Greenhorn
Posts: 8
Android Chrome Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Lalit. Welcome to the Ranch - I'm fairly new here, too, and also reading the "Head First Java" book.

The one question I can answer is that, yes, Java programs can and do contain multiple classes...including classes that define other classes (you'll learn these are defined as superclasses and subclasses). I'm not sure how far you've read in the book so I'll leave it at that so as not to confuse you.

As you probably have read, though, classes are the "definitions" of objects, or the "blueprints" of objects that set objects up with what they can do (methods) and what they can know (variables). The details get more intricate from there.

Concerning the structure of Java, can you clarify what you mean, exactly? Are you asking about how the Java code is written when you're actually writing the program, or something else? (I might learn something here, too.)
 
BalaMurali dhar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can do contain multiple classes in Java Program but public class should be one. Second thing is that Structure what exactly is the meaning ?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BalaMurali dhar wrote:you can do contain multiple classes in Java Program but public class should be one.

It is not true that a program should contain exactly one public class. Most programs contain many classes - hundreds or even thousands of classes. Many of those classes might be public.
 
BalaMurali dhar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can write Multiple classes in single Java program but if you write more than one public class it wont run
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BalaMurali dhar wrote:. . . if you write more than one public class it wont run
Nonsense!

Do you mean you can’t get more than one public top‑level class in one file? That would be correct.

In my opinion a Java™ application has at least two classes. One contains only a main() method which starts the application, and contains a single statement.
The other class/es have fields, constructors and methods. As Jesper said, you can have thousands of classes.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To clarify...a java PROGRAM is not the same as a java FILE. A program can be made up of classes in hundreds or thousands of files.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BalaMurali dhar wrote:you can write Multiple classes in single Java program but if you write more than one public class it wont run


Let me clarify it for you:
What you refer to as a "Java program" is more commonly called a "Compilation Unit" or "Java source file" or a ".java file"

In this respect you are correct: you can have multiple classes defined in one compilation unit but there can be only one top-level public class defined per compilation unit (a .java file) and the name of the public class MUST match the name of the source file.
 
Rose Ellis
Greenhorn
Posts: 16
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
BalaMurali dhar wrote:you can write Multiple classes in single Java program but if you write more than one public class it wont run


Let me clarify it for you:
What you refer to as a "Java program" is more commonly called a "Compilation Unit" or "Java source file" or a ".java file"

In this respect you are correct: you can have multiple classes defined in one compilation unit but there can be only one top-level public class defined per compilation unit (a .java file) and the name of the public class MUST match the name of the source file.


Although multiple 'inner' private classes are allowed in a single .java file, it's not recommended as it adds unnecessary complexity to someone trying to maintain your code. So if you can do without them being in one file, keep your classes separate. After all, java is an object oriented language, and a .java file normally represents a single object
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rose Red wrote:Although multiple 'inner' private classes are allowed in a single .java file, it's not recommended as it adds unnecessary complexity to someone trying to maintain your code. So if you can do without them being in one file, keep your classes separate. After all, java is an object oriented language, and a .java file normally represents a single object


Hi Rose,

Welcome to the Ranch!

I have to somewhat disagree with your comment regarding inner classes not recommended and contributing to unnecessary complexity: not necessarily so.

With judicious use, inner private classes can actually help reduce complexity and increase maintainability. If the scope of use of an inner class is truly limited to just the top-level public class or other private inner classes that they are with in the same compilation unit, then this promotes information/implementation hiding: a common strategy for curbing complexity. The problem arises only if the inner classes are lumped into the same compilation unit out of sheer laziness on the developer's part with no consideration for overall project structure and relationships between classes. One particularly bad habit is to automatically make a top-level class in a Java compilation unit a public class. Sometimes making the class public is not really appropriate for the scope of its responsibility and usage. I often like to start with a class that has a more limited scope, like package private (default) or private. With modern IDEs, it's very easy to refactor and increase the visibility of a class. Once you've made a class public, however, and it has already been referenced in many places, it can be more difficult to decrease the scope to improve the design.

One example of this is in the Data Access layer. I use Spring and I often define private helper classes to represent stored procedure calls, queries and the like to use with my DaoSupport classes. The helper classes are used only by the particular DaoSupport class that they are lumped with in a single compilation unit. This makes everything in the compilation unit highly cohesive, an attribute that programmers should always strive to have in their code.

You won't find me using any global static class (a very common anti-pattern) that has all the SQL query statements being used in my data layer lumped together as "public static final String" declarations. Each SQL query string is embedded directly in the private inner class that represents the data access operation (stored proc, query, insert, update, etc.) it will be used for. This strategy also forces me to make my Data Access objects truly reusable because it eliminates duplication and reduces the number of dependencies (what you get a lot of when multiple DAOs reference the same SQL statement constant in the global static class). When I find myself wanting to access a private inner class from a class that doesn't have access to it, I know it's time to refactor code. I find that this strategy keeps my Data Access layer packages much cleaner, coherent, and relatively free of clutter than if I had given all these helper/support classes their own .java files to live in.

Again, the key is to use inner classes judiciously and with mindfulness to scope and responsibility.
 
Rose Ellis
Greenhorn
Posts: 16
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did in fact have in mind those 'lazy' cases which I come across in my current project and find myself refactoring them anyway.

I am not experienced enough to see the good use for inner classes, but I do agree, the ability to create inner classes was added for a reason and I'll read into your comment a bit more to find out why. Thanks for the detailed example
 
lalit khera
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rose Ellis wrote:
Junilu Lacar wrote:
BalaMurali dhar wrote:you can write Multiple classes in single Java program but if you write more than one public class it wont run


Let me clarify it for you:
What you refer to as a "Java program" is more commonly called a "Compilation Unit" or "Java source file" or a ".java file"

In this respect you are correct: you can have multiple classes defined in one compilation unit but there can be only one top-level public class defined per compilation unit (a .java file) and the name of the public class MUST match the name of the source file.


Although multiple 'inner' private classes are allowed in a single .java file, it's not recommended as it adds unnecessary complexity to someone trying to maintain your code. So if you can do without them being in one file, keep your classes separate. After all, java is an object oriented language, and a .java file normally represents a single object


Thanks Lacar.

I want to ask you that why there is restriction that the class that contains the main method should be declared as public.
And why only one public class for each program.
Why dnt we write many public classes for one program.
for example:

package may26;
class Phone
{
public int modelno;
public String modelname;
public Phone(int no,String name)
{
modelno=no;
modelname=name;
}
public void setno(int no)
{
modelno=no;
}
public void setname(String name)
{
modelname=name;
}
public int getno()
{
return modelno;
}
public String getname()
{
return modelname;
}
}

public class Mainphone
{
public static void main(String uuiui[])
{
Phone p= new Phone(84,"Nokia");
/*System.out.println(p.getno());
System.out.println(p.getname());*/

System.out.println(p.modelno);
System.out.println(p.modelname);



}
}


it is working fine.
But when i change one line that is class Phone to public class Phone, it shows an error.
why???
 
Rose Ellis
Greenhorn
Posts: 16
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you put both classes in one file, which is called Mainphone.java

When you add the word 'public' to the class Phone, the compiler displays an error, because you can only define one public class per file and it must match the file name (Java rule), other classes in the same file have to be private or default (meaning no access modifier is used). When you try to define more than one public class in the same file, how do you think you would name that file? Two combined class names? That wouldn't match the Java rule, hence the compilation error.

Hope this helps
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you have this sort of question, the place to look is the JavaLanguage Specification. Scroll down to the paragraph starting “If and only if packages...” at the end. It isn’t easy to read.
There you see it says implementations may enforce a rule that the name of the .java file and the name of the public top-level class it contains must be the same. It doesn’t actually say all compilers enforce this rule; I have only used Sun/Oracle and Eclipse compilers, which do enforce that rule. It also explains why, and says you can’t get two public top-level compilation units into the same file.
Top-level classes must be public or have default/package‑private access, so protected, static and private can only be used of nested/inner classes.

Agree with Rose Ellise that putting those two compilation units in the same file and marking both public will cause a compile-time error.
 
lalit khera
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rose Ellis wrote:I assume you put both classes in one file, which is called Mainphone.java

When you add the word 'public' to the class Phone, the compiler displays an error, because you can only define one public class per file and it must match the file name (Java rule), other classes in the same file have to be private or default (meaning no access modifier is used). When you try to define more than one public class in the same file, how do you think you would name that file? Two combined class names? That wouldn't match the Java rule, hence the compilation error.

Hope this helps


thanks ellis.

Yes i created only file as Mainphone.java
You are absolutely correct and i agree too.
So you mean to say that we can have only and exactly one class as public per file.
And the rest of the classes are either private or default.
Thanks i understand that but how would i convince to someone else who is just a beginner in java or who wants to test my basic skills in java.
may be my student or interviewer.
can i explore this concept in detail.



thnks
 
lalit khera
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Whenever you have this sort of question, the place to look is the JavaLanguage Specification. Scroll down to the paragraph starting “If and only if packages...” at the end. It isn’t easy to read.
There you see it says implementations may enforce a rule that the name of the .java file and the name of the public top-level class it contains must be the same. It doesn’t actually say all compilers enforce this rule; I have only used Sun/Oracle and Eclipse compilers, which do enforce that rule. It also explains why, and says you can’t get two public top-level compilation units into the same file.
Top-level classes must be public or have default/package‑private access, so protected, static and private can only be used of nested/inner classes.

Agree with Rose Ellise that putting those two compilation units in the same file and marking both public will cause a compile-time error.


Thanks ritchie, now i understand the stuff.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!