please explain every element of Java main ("public static void main(String[] args)") - static

Explain Java main method?
2.Why main is always public in Java?
3.why main is always static?
4.why main always accepts String array as argument?
5.can we overload main method in java?
can we override main method in java?
Please explain in detail

Hi Please find the answers below:
1.Explain Java main method?
Please find complete explanation in following video : https://www.youtube.com/watch?v=Z7rPNwg-bfk&feature=youtu.be
2.Why main is always public in Java?
So that main can be accessed by JVM.
3.why main is always static?
at RunTime JVM do not create any object and static method can be accessed by class name, so main is always static.
4.why main always accepts String array as argument?
it is a command line argument and any data type can be converted to string just by putting it in double quotes, so string is used.
5.can we overload main method in java?
Yes
6. can we override main method in java?
No Static methods can not be overridden.

Related

Calling Apex method with multiple signatures from LWC

I've noticed some interesting behavior in an LWC that I am building and haven't been able to find much info on the cause. Basically I have an Apex method declared with multiple signatures:
// myMethod with 2 param signature
#AuraEnabled
public static void myMethod(String param1, String param2) {
// I expect this method to be invoked when called from the LWC code shown below
....
}
// myMethod with 3 param signature
#AuraEnabled
public static void myMethod(String param1, String param2, Boolean param3) {
// However this method gets invoked instead
....
}
When I attempt to call myMethod from an LWC, and only pass two parameters into the method, I would expect that the 2 param signature method would be invoked, however what happens is that the 3 param signature method is invoked, and the value of null is passed as the third params value.
runJob({param1, param2}).then(value => ... )
Is this expected behavior? When I call the methods through apex, the correct method is invoked by its signature, however this doesn't seem to be the case when calling the method from an LWC.
Is there a way for me to invoke the myMethod Apex method of the correct signature from an LWC?
Edit:
it will be banned at compile time in Summer'22 release. https://help.salesforce.com/s/articleView?id=release-notes.rn_apex_ValidationForAuraEnabledAnnotation.htm&type=5&release=238
Original:
It's worse than you think. runJob({param2, param1}) will work correctly too. "Correctly" meaning their names matter, not the position!
Your stuff is always passed as an object, not a list of parameters. You could have a helper Apex wrapper class and (assuming all fields are #AuraEnabled) it'll map them correctly. If you have list of parameters - well, a kind of unboxing and type matching happens even before your code is called. If you passed "abc" to a Date variable - a JSON deserialization error is thrown even before your code runs, you have no means to catch it.
https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.apex_wire_method
If the Apex method is overloaded, the choice of what method to call is
non-deterministic (effectively random), and the parameters passed may
cause errors now or in the future. Don't overload #AuraEnabled Apex
methods.
So... pick different names? Or funnel them so the 3-param version looks at the last param and calls 2-param one if needed and the business logic allows it. Generally - keep it simple, leave some comments and good unit tests or 2 months from now poor maintenance guy will struggle.
And (if you use the Apex PMD plugin and/or sfdx scanner) functions with long parameter lists are frowned upon anyway: https://pmd.github.io/latest/pmd_rules_apex_design.html#excessiveparameterlist
See also
https://github.com/trailheadapps/lwc-recipes/issues/196
https://salesforce.stackexchange.com/questions/359728/apex-method-overloading-is-not-working-when-called-from-lwc-javascript-controlle

Camel bean binding: set parameter from variable

Given a bean method that takes a String parameter:
public void emptyDirectory(String directory) {
// code to empty give directory if it exists
}
how do i pass this parameter? The method is called here:
String to = configuration.getTo();
from(configuration.getFrom())
.to("bean:splitFileByProductType?method=emptyDirectory(to)")
....
This doesn't work as 'to' evaluates to "to", and not the value of configuration.getTo().
The documentation does not mention a case like this, so i don't know if what i'm trying to do is even possible, for example in the Simple language.
I know the value becomes accessible if i add it to the exchange header or if i hardcode it.
You can pass a value as method argument with ${body}, ${body.NAME}, ${property.NAME} and ${header.NAME}.
Examples http://camel.apache.org/bean-binding.html
So first of all you have to put your variable in Camel exchange.
To pass information from the Camel Exchange to a bean method you must not add or change anything in the route.
If you have this route (from your question). Notice that if the bean has only one method you can omit the method name.
from(whatever)
.to("bean:splitFileByProductType?methodName=emptyDirectory")
Or alternative
from(whatever)
.bean(splitFileByProductType, "emptyDirectory")
You can annotate your bean method to get the needed Exchange information automagically:
public void emptyDirectory(
#Header("directory") String directory,
#Body String body
... [other stuff to be injected] ) {
// your method impl
}
See the Camel docs for more information about this feature.
You could assign the parameter to an exchange header or property and then use simple language to pass it the bean method
String to = configuration.getTo();
from(configuration.getFrom())
.setHeader("foo", constant(to))
.to("bean:splitFileByProductType?method=emptyDirectory(${header.foo})")
...
From documentation:
The only things that can be passed to a bean are Simple tokens, a String value, a numeric value, a boolean or null.
The variable from the example cannot be expressed using Simple. What can be expressed are various properties of the exchange, most notably the exchange headers, and also some random things like literally random numbers, the current date and so on. Check the documentation for more info.
I decided to treat the whole thing as a code smell, and moved the method to a utility class which i call prior to initializing the route.

How to insert print for each function of C language for debugging?

I am studying and debugging one software. There are thousands of functions in this software. I plan to add printf() at the entry and exit point of each function. It will take a lot of time.
Is there one tool/script to do this?
I may use '__cyg_profile_func_enter'. But it can only get address. But I have to run another script to get function name. I also hope to get value of input parameters of this function too.
You should give a try to AOP : Aspect Oriented Programming. Personnaly I've only tried with Java and Spring AOP but there's an API for C too : AspectC (https://sites.google.com/a/gapp.msrg.utoronto.ca/aspectc/home). From what I've seen, it's not the only one.
From what I've red about this library, you can add an pointcut before compiling with AspectC :
// before means it's a before function aspect
// call means it's processed when a function is called
// args(...) means it applies to any function with any arguments
// this->funcName is the name of the function handled by AspectC
before(): call(args(...)) {
printf("Entering %s\n", this->funcName);
}
(not tried by myself but extracted from the reference page https://sites.google.com/a/gapp.msrg.utoronto.ca/aspectc/tutorial)
This is only a basic overview of what can be done and you still have to deal with the compilation (documented in the page linked before) but it looks like it could possibly help you. Give a try with a simple POC maybe.

Accessing an array without static references in another class (OOP)

I'm new to object-oriented-programming, and I have set a strict goal for myself for my current project which is not using static variables. In the process I will try to learn about OOP.
I'm using the Haxe language and it's one of the best languages I've ever seen.
I know a bit about C pointers, and that pointers only store the address of a variable so its pretty much the same variable, just taking up less space (especially for large variables).
Now back to the present, I want to have pointer references to an array of objects of one class because
I want these objects to interact with each other,
and I don't want to have any static references,
and I don't want to have every object holding a copy of that array.
How should I go along accessing this array?
Or is there another OOP design pattern or something?
Please correct me if I got something wrong.
There are many questions in this brief.
You can just pass along a context along all your variables, it will be passed by pointer
class Context{
var level:Level=null;
var enemies:Array<Enemy>=[];
}
class Enemy{
var ctx:Context;
function new(ctx){ this.ctx=ctx; }
}
class Main{
static function main(){
new Game(new Context());
}
}
and in game, pass along the context to everyone :
new Enemy(ctx);
etc...
Frankly it is often easier to use static for contexts, like
class Context{
static var level:Level;
}
But that's up to you :)
As a side note, all non primitives are pointers to structure (ex arrays) just like java.

What is significance of static keyword in Java and in C++?

What is the importance of Static keyword in Java and in C++ and how it's functionality differ in both programming languages ?
Maybe this link will give you a better idea: http://www.pp.rhul.ac.uk/~george/PH2150/html/node48.html
It has a visual diagram that may make it easier to understand.
There are 2 meanings for static. The first if you have a static variable, this means there is only 1 instance of this variable. It works pretty much the same in all programming languages with the keyword.
A static function is a function that can be called, even if the class it resides in is not instantiated. Static functions are necessary in C# and Java because you cant declare functions in these languages which have no encompassing class.
in C++, you can declare functions in the global namespace. In this language, static functions are used to denote that a function belongs to the class, but you dont have to instantiate the class to use the function. You could use a static function to access private variables of the class. Also note that in C++, static functions have a known memory address, so you can use function pointers to point to them without instantiating the class.
For Java, Understanding Instance and Class Members is a good place to start.
For C++, Microsoft has a reference on the static keyword.
There are many readily available programming language resources that will help you understand what the static keyword means. The above are two of them that I found with a quick Google search.
Use static for fields and methods that can only have one instance. That means they are not relevant to instances of a class, but to the class itself. For example the main thread (public static void main).
It works the same way in both languages. I assume you know what's object-oriented programming, and what's the difference between classes and objects/instances. So, if you mark a method or variable as "static", it operates on a class level, not instance level. All objects/instances share the same value of the "static" variable.

Resources