• 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

How to use anonymous classes in java

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to perform some rule checking in my application. I am using an inner interface(Check) within a class and am creating anonymous classes of the type interface(Check). Using this approach, the idea is to create an anonymous class for each rule. Please see code below:



But, I am not sure how to call the methods of anonymous classes from another class? Say, there's another class called TypeCheck. How do I call the IS_NUMBER.check method?


I have never used interfaces with anonymous classes before and I am trying this out. Thank YOU!
 
Marshal
Posts: 22405
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just like any class that implements Check, you can simply use a Check reference and call the method(s) from Check:
 
Marshal
Posts: 73751
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the Check instances should probably be marked final.

How are you going to check whether a String is a regular expression? That seems impossible to me.
 
sony vijay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell Ritchie: I would use the the java regex Patterns to check whether a string is a regular expression:

import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

I would do a Patter.compile(input) within a try-catch block. The catch block would throw a PatternSyntaxException. I hope you get the general idea. Please let me know if you need more explanation. But, I am not sure whether the the java Pattern of regular expressions matches the javascript regular expression patterns. I am yet to check on that.
 
Campbell Ritchie
Marshal
Posts: 73751
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see what you mean, but you can only check for syntax errors, not what the regular expression will test for.
 
sony vijay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! That was a useful link. But, I am trying to just check the syntax errors for now. Any idea if there are any differences between Java and Javascript regular expressions?
 
Campbell Ritchie
Marshal
Posts: 73751
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are bound to be different. Start looking here.
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic