Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am reviewing a new project code. I found the following line:
crudService.findByNamedQueryFirstResult("TableA.findby.custid", with(
"custID", IndividulaID).and("flags", custflags).parameters())
I don't see methods "with" and "and" in any class. And its not string concatenation.
I know a JPQ query has with and and. But how is it possible to pass params like above?
Can someone help?
There are no with or and keywords in Java. Those are method calls.
Here, the with() method is imported via a static import (see #damo's comment) and creates an instance of a builder class; the and() method is a method which returns this; the .parameters() class will then build the class, and return what the calling method actually needs. This is a classical builder pattern.
Static imports can be used to great readability effects. Consider mockito. Either you write:
Mockito.when(xxx).then(xxx);
or:
import static org.mockito.Mockito.*;
// the compiler know where `when()` comes from
when(xxx).then(xxx);
The generic pattern for such constructs is called a fluent interface. To complete the answer, I'd just point to what I have done for one of my projects:
ProcessorSelector<IN, OUT> selector = new ProcessorSelector<IN, OUT>();
selector = selector.when(predicate1).then(p1)
.when(predicate2).then(p2)
.otherwise(defaultProcessor);
final Processor<IN, OUT> processor = selector.getProcessor();
The ProcessorSelector class has a when() method; this method returns another class on which there is a then() method; and the then() method returns, again, a ProcessorSelector, which has when(), etc. Finally, the otherwise() method returns a ProcessorSelector on which you have a .getProcessor() returning a Processor.
This looks like a fluent-api (see http://java.dzone.com/articles/java-fluent-api-designer-crash for general definition)
In your case it may be spring-jpa, QueryDSL or similar framework.
To identify the API, you should have a look at the type/superclass/interfaces of crudService as well as the definitions of with/and/parameters.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm new with Swift 2, I never had developed in Apple. Objective C has seemed ugly and you have to write a lot of code, so Swift liked because the syntax is similar to Java and C and here my question:
In Java you can define a class of this way:
new Thread(){
public run(){
// anything
}
}.start();
Then, Swift can do this? And how?
Thank and greetings
Solution
let myThread=NSThread(target: self, selector: "backgroundWork", object: nil)
myThread.start()
}
func backgroundWork(){
for (self.counter=0;self.counter<10;self.counter++){
//self.myLabel.text = String(self.counter) // Not update UI
self.performSelectorOnMainThread( Selector("updateLabel"), withObject: nil, waitUntilDone: false )
sleep(3)
}
}
func updateLabel(){
self.myLabel.text = String(self.counter)
}
Mostly this syntax (inline definition of anonymous classes) exists because Java doesn't allow the concept of closures or lambda functions, so if you want to pass a function to be invoked you have to pass an instance of a class with the function then declared inline.
In contrast, Swift, like most modern languages has a specific closure syntax which allows you to directly pass executable blocks to another routine, essentially it allows you to treat functions as first class language entities.
So, the bottom line is that no, Swift doesn't allow the construct you've asked about, but it does provide equivalent functionality for the dominant use case. The code most analogous to your example would be:
dispatch_async(...) {
Code to be executed asynchronously here
}
Which is really just syntactic sugar for:
dispatch_async(..., {
Your code here
})
Since the anonymous object is being created only as a holder for a single object, there's really no need for the object, or indeed, the class, and hence the syntax. The only time the Java syntax has a slight advantage is if the callee needs to maintain multiple related callbacks.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In our code we are supposed to have an Interface (let's call it InterfaceMap) that has the methods that let you put a cell in a Sheet (putCell(), getCell(), etc).
We have a class called OurSheetMatrix that implements that InterfaceMap. Can we have something on a different class Sheet like:
public class Sheet {
private InterfaceMap m = new OurSheetMatrix();
...
}
I think we might be able to do this not with Interfaces, but with abstract classes. But now, I'm not sure.
Yes. OurSheetMatrix is-a InterfaceMap. You could not however say new InterfaceMap(); because you can not instantiate an interface directly (It's probably best to think of them as promises [or more formally contracts]). Abstract classes are similar but distinct, it's the relationship that matters (and they can't be directly instantiated).
Yes you can do it :)
private InterfaceMap m = new OurSheetMatrix();
is OK. You can also pass references to your interface around in methods:
public void doSomething(InterfaceMap iamp) {
//Do something with an InterfaceMap.
//I don't know (or care) exactly what class it is,
//so long as it implements InterfaceMap
}
But if you have something more specific:
public void doSomethingElse(OurSheetMatrix matrix) {}
It cannot be called like this:
InterfaceMap imap = new OurSheetMatrix(); //this is OK
doSomethingElse(imap); //But this? NO! can't do this!
The above call to doSomethingElse would not compile because doSomethingElse wants a OurSheetMatrix. Although we know it's really a OurSheetMatrix the method does not know.
All OneSheetMatrix objects are also InterfaceMaps, but InterfaceMaps are not necessarily OneSheetMatrix objects, hence the call to doSomethingElse is not valid - there may be other classes which implement InterfaceMap.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have to create a class. Instance of this class cannot be made. How can I achieve that?
Declare it abstract and add a private constructor.
Do you mean no instances can ever be made? or just one instance?
If no instances can ever be made, then make the class final with a private constructor. All methods then need to be static. A good example of this is java.lang.Math
If you need only a single instance and want no one else to make new instances, then consider using an enum as described in Effective Java 2nd ed:
public enum MyClass{
INSTANCE;
...methods
}
Code that uses this class then invokes methods like this: MyClass.INSTANCE.foo().
If you don't want to instantiate that class then I assume that you only want it to have some static methods. In that case you can easily do it with enum with no constants like
enum MyUtilities{
;//if you want you can place instances of that class here, or not place any
//your methods
public static void myMethod(){
System.out.println("hello");
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I was wondering if I could use this. to call a non-static method from within a static method. I know I would need an object in general to reference to a non-static method from within a static method. Thanks
No. Within a static method, this has no meaning and will not compile. This is covered by §8.4.3.2 of the Java Language Specification, although it should be fairly intuitive — what would thisrefer to?
You can call non-static methods from static methods, just not via this. You have to have an instance on which to call them.
You cannot use "this" keyword in a static method.
The answer is NO. A static method is not associated with an instance of the class, so it cannot access a non static variable or method of the same class that has a meaning only if there is an instance of the class
Not using this, but if you really wanted to, if the class was named MyClass, you could do
new MyClass().someNonStaticMethod()
But if you're calling instance methods like this, they should probably be static anyway.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am looking for a ans what I have found in facebook and became confused
"is it possible to declare a constructor inside a method"
Short answer: no.
Long answer: This comes from the Java Language Specification, §8.8:
A constructor is used in the creation of an object that is an instance of a class.
In all other respects, the constructor declaration looks just like a method declaration that has no result (§8.4.5).
Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding.
It's declared like this:
ConstructorDeclaration:
ConstructorModifiers(opt) ConstructorDeclarator
Throws(opt) ConstructorBody
ConstructorDeclarator:
TypeParameters(opt) SimpleTypeName ( FormalParameterList(opt) )
A ConstructorDeclarator can only live inside of the class body declaration:
ClassBody:
{ ClassBodyDeclarations(opt) }
ClassBodyDeclarations:
ClassBodyDeclaration
ClassBodyDeclarations ClassBodyDeclaration
ClassBodyDeclaration:
ClassMemberDeclaration
InstanceInitializer
StaticInitializer
ConstructorDeclaration <--
ClassMemberDeclaration:
FieldDeclaration
MethodDeclaration
ClassDeclaration
InterfaceDeclaration
;
A MethodDeclaration has no symbol to a ConstructorDeclaration, which is why you cannot declare a constructor inside of a method.
You cannot declare a constructor inside a method.
Constructors and methods are both components of objects. Your object has a constructor that gets called when you instantiate it. Once instantiated, your object then has properties to define it, and methods to do things with.
If you try to place a constructor into a declaration, you will get a compilation error.
This comes from several years of practice and study for the Sun certification tests.
No it is not possible.java does not support inner methods concept.Constructor is also like a method.Constructors can be declared inside a class only.
The constructor is sort of a method much like the main method. The constructor is used to initialize an object of the class and it has no return value.
Not at all possible.
A constructor must invoked while creating object for a class.
With out instance how you will call that method,which is building the constructor?.
Making any sense ?