• Post Reply Bookmark Topic Watch Topic
  • New Topic

question about the class dog example in 3rd chapter of Head First Java  RSS feed

 
Nabanita Sarkar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the example:



======
Now my question is till the 2nd chapter i came to know that there is always two classes.one is tester class with main method and the other one is my own created class with instance variables and methods..and in the tester class we create objects ,which can access variables and methods of my own created classes .

But in this example methods and variable is defined and declared in the same tester class itself..how and why...can anyone explain me on this???...thanks
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be sure to always UseCodeTags (← that's a link). I'll do it for you this time.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In projects with many class files, there is only one that has a main() and this usually only launches the program, so most classes don't have a main().

This class is just a quick example, so the authors have put the main method in with the class. They could have used two classes but this is just an example. In large projects you wouldn't do this.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
main method is also an method so it can be inside one class with other methods.There is no problem with it

As you are at chapter 3 .so I am assuming you are still at beginning
Stage
So when you'll be reading about access modifiers
You'll understand what advantage you have while accessing private field,with main method inside
 
Nabanita Sarkar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:In projects with many class files, there is only one that has a main() and this usually only launches the program, so most classes don't have a main().

This class is just a quick example, so the authors have put the main method in with the class. They could have used two classes but this is just an example. In large projects you wouldn't do this.



Thanks for the info
 
Nabanita Sarkar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:main method is also an method so it can be inside one class with other methods.There is no problem with it

As you are at chapter 3 .so I am assuming you are still at beginning
Stage
So when you'll be reading about access modifiers
You'll understand what advantage you have while accessing private field,with main method inside


thanks...My query was not for the methods defined inside the class...actually i was confused about creating the objetcs..

My question was :can we create the objects in the same class where we define the object's variables and methods???
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nabanita Sarkar wrote:My query was not for the methods defined inside the class...actually i was confused about creating the objetcs..

My question was :can we create the objects in the same class where we define the object's variables and methods???

Yes. Here's a simple "singleton" class that does just that.

or, creating one inside a method.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have missed the point
I was telling you the same.
As you are creating object of a class in a main method .So you can have a main method inside the class where you have defined classes other fields and can create its object there(in main method)
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create object in main method or in 2 ways as suggested by Carey
 
Nabanita Sarkar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Nabanita Sarkar wrote:My query was not for the methods defined inside the class...actually i was confused about creating the objetcs..

My question was :can we create the objects in the same class where we define the object's variables and methods???

Yes. Here's a simple "singleton" class that does just that.

or, creating one inside a method.





Thanks ...

i would like add one more question about public class:

Usually we use public class and pulic static main method for the Tester class...But when i declare other classes as public other than the tester class ...there is always a compiler error...and it asks for the .class file for other classes as well....why???
 
Nabanita Sarkar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:You can create object in main method or in 2 ways as suggested by Carey


Thanks
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually we use public class and pulic static main method for the Tester class...But when i declare other classes as public other than the tester class ...there is always a compiler error...and it asks for the .class file for other classes as well....why???

Are the two public classes in the same file? You can only have one public class per physical file in Java.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing to worry ,about what you are thinking
There is no rule as such that a class can be public only when if it has a public method.(If this is what you were thinking)
One thing more you can only have one public class ( along with other non public class) in a .java file.Name of your .java should be as same as your public class name
 
Nabanita Sarkar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:There is nothing to worry ,about what you are thinking
There is no rule as such that a class can be public only when if it has a public method.(If this is what you were thinking)
One thing more you can only have one public class ( along with other non public class) in a .java file.Name of your .java should be as same as your public class name



Thanks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!