1

I want to create an custom datatype in Java,for example datatype Email , that having following method isValidate(String email),isEmailExist(String email),getDomain(String email), get Id(String email),just like Integer class in java.

Integer is a class and I can initialise the object of Integer class as follows:

Integer i = 100;

I created my class Email and I want to initialise it as follows

Email e = "sam";

How can i perform this functionality in my Email class.

import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Email { private String email; public Email(String email) { this.email=email; }

Email() {  
}

public Boolean isvalid(String email)
{

String lastToken = null; Pattern p = Pattern.compile(".+@.+\.[a-z]+"); // Match the given string with the pattern Matcher m = p.matcher(email); // check whether match is found boolean matchFound = m.matches(); StringTokenizer st = new StringTokenizer(email, "."); while (st.hasMoreTokens()) { lastToken = st.nextToken(); }

if (matchFound && lastToken.length() >= 2 && email.length() - 1 != lastToken.length()) { return true; } else return false; } public String toString() { return email; } }

Thanks

3
  • 1
    You'll need to learn the basics of creating a class in Java. Here's an online tutorial: Classes in Java Commented Feb 9, 2011 at 6:52
  • 1
    The only thing that you won't be able to do is Email e = "sam";. You will need Email e = new Email("sam"); instead. Commented Feb 9, 2011 at 7:39
  • I think that the OP is asking how to do something known as "implicit operator overloading". That would make this question a not-so-beginner question after all. In Java this is noticeable when using the wrapper types. When you assign a literal to a java.lang.Integer for example. The answer is that Java does not support creating custom types that allow this. So you cannot do Email email = "[email protected]". Commented Nov 11, 2012 at 8:42

2 Answers 2

6

You cannot instantiate it as you write, the closest would be using a constructor:

Email e = new Email("Sam");
Sign up to request clarification or add additional context in comments.

Comments

3

Create an Email class. Java 101; any book or free tutorial of the Java language will get you started.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.