• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubts regarding Default access of class  RSS feed

 
Nishant Kumar Singh
Greenhorn
Posts: 8
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am new to Java and started learning it a few months back.Please clarify why i am getting error when i am calling iad.abc() from TestAccessModifiers class.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch! I added code tags to make your code easier to read.

The problem isn't the default access modifier. The problem is that the line iad.abc(); isn't in a constructor, method or "block. Either of these compile:



 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch..

Always use code tags for pasting Java code. Secondly, write code in proper indented manner. Look at the following:



Makes it much more clear. Lastly, name your classes properly. I have changed the names. First let me ask you this. Do you know the fundamentals of the language? Which book are you learning from?

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind,
Your post was moved to a new topic.
moved discussion about how to moderate to a new thread
 
Nishant Kumar Singh
Greenhorn
Posts: 8
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:Welcome to the ranch..

Always use code tags for pasting Java code. Secondly, write code in proper indented manner. Look at the following:



Makes it much more clear. Lastly, name your classes properly. I have changed the names. First let me ask you this. Do you know the fundamentals of the language? Which book are you learning from?


Thanks for your prompt reply!!!
I have started learning Core Java from SCJP book of Kathy Sierra.I know i am weak at fundamentals right now but i'll improve it.But i am finding Core Java somewhat difficult. In college i learned language C and also did 8085 microprocessor programming and i was very good at it.But Java seems to be completely diffrent language.So many things to learn and have to burn in mind!!
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you get a firm grip on the fundamentals first. Leave the SCJP book aside for the moment. Certifications can be completed later. Study from "Head First Java". It's by the same authors. But it is written with an aim of teaching Java( not to clear the SCJP exam). It is important that you have the basics crystal clear. Don't worry if you are having a hard time at understanding the terminology and concepts initially. That is normal. But make sure your approach to learning is correct. You have any doubts, we are here to guide you. But you need to make the effort. Jeanne answered your question. Have you understood why the compiler was complaining?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nks Singh wrote:In college i learned language C and also did 8085 microprocessor programming and i was very good at it. But Java seems to be completely diffrent language...

Yup. Completely different. Even though it looks similar.

For one thing, it's an Object-Oriented language, so if you're not familiar with the differences, learn them (maybe a bit at a time, because most books won't teach you this; they will simply present you with an "objective" view of the world).

The only things I can advise you about, as a former C programmer myself:
1. DON'T obsess about memory. It's the JVM's business, not yours.
2. Efficiency is NOT something you should worry about. You've chosen Java; just assume that it's less efficient, and deal with designing the correct solution to your problem (something that C might not have allowed you to do).
3. Java enums are NOT the same as C's; and for a good reason.
4. Global variables are BAD.

And I'm sure there are plenty more, but hopefully it's a reasonable start.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
2. Efficiency is NOT something you should worry about. You've chosen Java; just assume that it's less efficient, and deal with designing the correct solution to your problem (something that C might not have allowed you to do).


I have a slightly different take on it. Whether it's C or Java, if you're writing application software, focus on efficiency in the right context. Make sure you're using appropriate architecture, design, data structures and algorithms, e.g. use an RDBMS rather than a bunch of flat files for data with complex relationships, use a Set rather than linear traversal of a List to find duplicates, etc. Don't worry about shaving of a byte here or a microsecond there in either language, unless you're writing something close to the hardware like a device driver (which you're not going to write in Java anyway).

And with that view, I would not assume that Java is "less efficient." In the context of an application that's too broad a statement to make about Java vs. C, and depending on how you choose to measure it, it may be that Java is more efficient, less efficient, the same, or can't tell.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
use a Set rather than linear traversal of a List to find duplicates, etc.

yes or Map
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:
Jeff Verdegan wrote:
use a Set rather than linear traversal of a List to find duplicates, etc.

yes or Map


If you need key/value pairs, then, yes, a map, but if it's just a collection of objects, a Set is more appropriate.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote: but if it's just a collection of objects, a Set is more appropriate.

O(1) so there is no problem to go extra key, till Collection .I wonder why they(java implementer) did not include it in collection(may be coice ;) ). you have many rooms, then keys are also collection
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:
Jeff Verdegan wrote: but if it's just a collection of objects, a Set is more appropriate.

O(1) so there is no problem to go extra key,


A Set is still more appropriate. Not for performance reasons, but because it is a better model for what we're doing. Performance should come second to good design unless and until it is proved by measurement that the good design leads to unacceptable performance.

I wonder why they(java implementer) did not include it in collection


Because a Collection is defined to be a group of individual items, whereas a Map is a group of key/value pairs. It could have been made to work, but it wouldn't be a good fit.

you have many rooms, then keys are also collection


Yes, and Map's keySet() method returns a Set (which is a Collection) of keys. But the Map doesn't just hold the keys; it holds the key/value pairs.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!