This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

The public type conflicts must be defined in its own file  RSS feed

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was the error message that shows for Conflicts

The issue here is

i wanted to check out how both

java.util.Date and java.sql.Date can be used in a code

i had two ways of doing it as per my knowledge that being

import java.util.Date

public class conflicts
{

Date date;

java.sql.Date sqlDate;

}

or

public class conflicts

{

java.util.Date date;
java.sql.Date sqlDate;

}

i get an error under conflicts stating "The public type conflicts must be defined in its own file".

How to go about it.



 
Stephan van Hulst
Saloon Keeper
Posts: 7812
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Public types need to be declared in a source file of the same name. If your class is named "conflicts", the source file should be named "conflicts.java". Note that you should always start type names with a capital letter.

By the way, you shouldn't use java.util.Date anymore. Instead, use java.time.LocalDateTime or java.time.Instant.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:
By the way, you shouldn't use java.util.Date anymore. Instead, use java.time.LocalDateTime or java.time.Instant.

This is assuming you're one of the lucky folks that gets to use Java 8. If you are stuck on an older version of Java, like some of us, look at the Joda time libraries. They were the source and inspiration for the improved Date classes in Java 8.
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
guess i need to update from java 7 to 8 to use java.time

what is the practical way of explicitly using java.util.Date and java.sql.Date in a program without any conflicts, are 2 ways that i mentioned the only or is there any other way as well assuming the java version to be 7
 
Stephan van Hulst
Saloon Keeper
Posts: 7812
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, if you want to use two classes with the same name in the same file, at least on of them needs to be fully qualified. If you really have to use both at the same time, you should always fully qualify both of them so there's no confusion which one you're using at any given moment.
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help Stephan / Kevin

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!