• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Two Packages Same Classes and Enums

 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have got a problem

I have got a Jar which i need to implement in project

That jar have packages like this

org.company.domain.xml.v03
org.company.domain.xml.v02

Both packages have some common classes like

org.company.domain.xml.v03 contains
CompanyIdentifier
CompanyClassifier
CompanyDemographicInfo

org.company.domain.xml.v02 also contain
CompanyIdentifier
CompanyClassifier
CompanyDemographicInfo

Now in my main program
i need to declare object of both classes(seperate packages) as



and



This seems to me disgusting
______________________

I could not write Import for these two as both are using same class as class collision is there

I also tried to write



and declared as



none of them is working as it is saying v03 or v02 are not resolved type

please suggest where am i doing wrong

____________________

Admins should this thread be in Java in General or Beginning Java
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Azrael Noor wrote:This seems to me disgusting


What's disgusting about it?
 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Standard, we need to declare objects on top and things are going somewhat like this:


and there are multiple entries related to this inside a function, even if i break it half of content goes here and there

still question is same that in such case
should i have to declare things like that only, any alternative

Readility Effects, new bie reading the code will get out of his head or isn't he

 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you were reading a book and it said 'The elephant has bigger ears than the elephant' it would make no sense. The sentence would need to be clarified to 'The african elephant has bigger ears than the indian elephant'. It's the same with source code. If just the class name on its own is ambiguous you need to clarify it by including the full package name. The only alternative is to refactor your source code so that references to both of the ambiguous classes don't appear in the same source file.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Azrael Noor wrote:should i have to declare things like that only, any alternative...


Not that I know of; but rather than banging you head against the wall and being disgusted with Java, how about thinking up some creatively painful things to do with the reproductive parts of the person who wrote the monstrosity that put you in this mess in the first place?

Winston
 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rather than banging you head against the wall and being disgusted with Java


i wanted to say is going disgusting not java which is B&B

how about thinking up some creatively painful things to do with the reproductive parts of the person who wrote the monstrosity that put you in this mess in the first place?




Still i am able to minimize it like following, it is looking




i think following could work but no it does not



The only alternative is to refactor your source code so that references to both of the ambiguous classes don't appear in the same source file.



I have no approach to Source Code and lets say if i decompile it and rewrite all confuse things again then i will get a hard kick on my ...



 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Azrael Noor wrote:

The only alternative is to refactor your source code so that references to both of the ambiguous classes don't appear in the same source file.


I have no approach to Source Code and lets say if i decompile it and rewrite all confuse things again then i will get a hard kick on my ...


I was suggesting you refactor your own code, not the code of the jar file. It should be possible to change your code so that v2 and v3 classes are never referenced from the same source file.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to use direct references, I don't think there's much you can do with the imports and having to explicitly type in the fully qualified class names for all but one of the packages. That's just the way Java works. You could write some new "bridging" classes that simply extend the other classes directly but give them different names. Then use the bridging classes instead. Since they extend the other classes, you can still use them the same way you would use the other classes but you don't have to use the old names or the fully qualified names. If you have many classes that have the same problem, put the bridging classes in a package of their own and import that package.

If the old classes have anything other than no-argument constructors, you'll have to write those in the bridging classes too, at least the constructors that you plan to use. In those constructors, you simply call the superclass version with the same parameters. Of course, this will only work if those classes are not declared as final. If they are final, it can still be done but with a little more work. You have to write classes that wrap and dispatch. Or maybe use Dynamic Proxies. One of these should do the trick.

To digress a little bit though, if you're really interested in improving the readability of your code, you should remove the "obj" warts in front of all your names. I have never understood what the point is of putting "obj" in the name of an object reference. Hungarian notation has no place in Java code. DON'T USE IT!

Give names that reveal intent, not implementation. The code below tells me nothing more than what the declaration already does. It's redundant. It's like someone asking you "What is 'discombobulated'?" and you answer, "Well, 'discombobulated' means that you're discombobulated!" Yeah, that's helpful and crystal clear now

And one more thing: I would question the "convention" of declaring everything at the top. Declarations should be placed as near as possible to where the variables are used so that the scope of usage is clear. You should always try to limit the scope of variables and references as much as possible. Too wide a scope makes it harder to track down bugs and makes refactoring more difficult. Having many declarations at the top of a class is, at the very least, reason to pause and consider if you are giving the variables too wide a scope or if your code is way too "busy" (inappropriately poking into other classes when it shouldn't be).
 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr. Lacar thank you for nice suggestions

What i got in your post is

1) To Declare Variables on Top with null and use somewhere in bottom is old style so called hungarian notation which should be avoided
2) Use variable where you use it and always assign NEW classname() instantiate class then to first declare as null then instantiation at particular portion


You could write some new "bridging" classes that simply extend the other classes directly but give them different names. Then use the bridging classes instead. Since they extend the other classes, you can still use them the same way you would use the other classes but you don't have to use the old names or the fully qualified names


i have written some function in single class, the class related to one part of domain only like



i have limited number of classes and could not extend.


Now i am using like this where i need that variable





Part of standards use obj before object name. the person who told to do so done coder of 90's :p
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Azrael Noor wrote:
1) To Declare Variables on Top with null and use somewhere in bottom is old style so called hungarian notation which should be avoided


Those are two separate issues. Hungarian notation is called such because it was developed by Charles Simonyi. The name objRTyping is an example of a name that uses Hungarian notation where "obj" is intended to indicate the type of the reference. This is not needed in Java because most everything is an object anyway. The 'obj' in the name just makes it more difficult to read the code.

As for your original question and your current approach, I don't understand it fully but if it works for you, I guess that's fine.
 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Azrael Noor wrote:
1) To Declare Variables on Top with null and use somewhere in bottom is old style so called hungarian notation which should be avoided


Those are two separate issues. Hungarian notation is called such because it was developed by Charles Simonyi. The name objRTyping is an example of a name that uses Hungarian notation where "obj" is intended to indicate the type of the reference. This is not needed in Java because most everything is an object anyway. The 'obj' in the name just makes it more difficult to read the code.

As for your original question and your current approach, I don't understand it fully but if it works for you, I guess that's fine.



well thank you

what i could add to my code is to stop declaring everything on top
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Azrael Noor wrote:
Part of standards use obj before object name. the person who told to do so done coder of 90's :p


He needs to get out of the 90s and throw away those old, outdated habits. Hungarian notation in Java code IS NOT GOOD and it is an anacronism in a coding standard for a language like Java.
 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

He needs to get out of the 90s and throw away those old, outdated habits. Hungarian notation in Java code IS NOT GOOD and it is an anacronism in a coding standard for a language like Java.



he did coding in c/c++ during his times... , mostly approach used in coding is inclined to c cpp

even i forget using term method though coding in java, we use term functions

a nice mingling of technologies out there

its code ranch where i put such question and get sweet knowledgeable kicks which lead to some improvements
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Azrael Noor wrote:
1) To Declare Variables on Top with null and use somewhere in bottom is old style so called hungarian notation which should be avoided


Those are two separate issues. Hungarian notation is called such because it was developed by Charles Simonyi. The name objRTyping is an example of a name that uses Hungarian notation where "obj" is intended to indicate the type of the reference. This is not needed in Java because most everything is an object anyway. The 'obj' in the name just makes it more difficult to read the code.



Read this entertaining article by Joel Spolsky. He says that Simonyi's intent was never to indicate type, but something far more useful, and it was a misunderstanding of this that, sadly, won widespread adoption.
 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr. Lacar

I am confused with one thing. You said me to remove variable declerations on top of function

as i have a function which is around 100 lines, i already shorten it by making many functions still it is more.
now for that 100 line function i declared things like




1) This way to declare variable then instantiate then use is not correct? So limited the scope. I limited there scope by declaring some things inside if statement or while loops where they are to be used
2) Instantiation should be done directly not like, first do null or then instantiate. even in loop directly instatntiation should be done if everytime we need to give new value to object


Is this fine?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without seeing the rest of the code that uses those variables, it's hard to tell whether what you're doing is appropriate or not. Based on my experience, however, it is probably the case that your method (not function) is doing way too much than should be allowed in a single method. Again, I can't really comment with any degree of certainty unless I get to see the rest of the code that uses all these variables.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic