Method overloading- cant find symbol - find

public class Overloading {
static void printing() {
System.out.println("Something being printed here");
}
static void printing(String name) {
System.out.println("hello"+name);
}
public static void main(String[] args) {
printing();
printing(rizwana);
}
}
I am trying something to check method overloading. But here is the error I got.
error: cannot find symbol
printing(rizwana);
symbol: variable rizwana
location: class Overloading

rizwana refers to a variable which you have not made in your code. If you want to pass a string, you should pass it as "rizwana".
Call the method as: printing("rizwana");

printing(String name) method will accept a string argument. So you should use printing("rizwana") instead of printing(rizwana). You could this
class Overloading {
static void printing() {
System.out.println("Something being printed here");
}
static void printing(String name) {
System.out.println("hello "+name);
}
public static void main(String[] args) {
printing();
printing("rizwana");
}
}

Related

How to set values of global variable inside inner interface and use the values outside of the interface in adnroid

Is there any way to set value of global variable inside an interface and use the value outside of it?
Here is sample of my code:
class A{
static ArrayList<String> myList = new ArrayList<>();
public static void main(String[] args) {
get(new Inter() {
#Override
public void callBack(ArrayList<String> list) {
myList = list; // here list is the textual data from firebase and I want to use it outside
// this get function call
}
});
myList.toString(); // I want to use it here. But its value out of the function call was null
// but it has the same value with list inside the function call
}
static void get(Inter inter){
// here I want to get some textual data from firebase into ArrayList of String
list = array of String from firebase
inter.callBack(list);
}
interface Inter{
void callBack(ArrayList<String> list);
}
}
You can't use the list directly after the get() call, because the get call is async and has not yet finished.
You can create a method outside the interface that takes the list as a parameter:
public static void main(String[] args) {
get(new Inter() {
#Override
public void callBack(ArrayList<String> list) {
doSomethingWithList(list);
}
});
}
private static void doSomethingWithList(List<Sting> list){
//you code here
}
Or you can use method-reference:
public static void main(String[] args) {
get(<your class>::doSomethingWithList);
}
private static void doSomethingWithList(List<Sting> list){
//you code here
}

Guava EventBus Multiple subscribers same tpe

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
public class Test {
public static class Processing { }
public static class ProcessingResults { }
public static class ProcessingFinished { }
public static EventBus bus = new EventBus();
#Subscribe
public void receiveStartRequest(Processing evt) {
System.out.println("Got processing request - starting processing");
}
#Subscribe
public void processingStarted(Processing evt) {
System.out.println("Processing has started");
}
#Subscribe
public void resultsReceived(ProcessingResults evt) {
System.out.println("got results");
}
#Subscribe
public void processingComplete(ProcessingFinished evt) {
System.out.println("Processing has completed");
}
public static void main(String[] args) {
Test t = new Test();
bus.register(t);
bus.post(new Processing());
}
}
So, in above example, it can be seen that there are 2 subscribers accepting same type Processing. Now, at the time of post(), which all functions will get called? If the 2 functions receiveStartRequest and processingStarted will get called, then in which order they will be get called?
Both your methods will be called and in no predefinite order.
To counter this, just create two extra classes: ProcessingStarted and ProcessingRequested.
public class ProcessingStarted {
private Processing processing;
// Constructors
// Getters/Setters
}
public class ProcessingStarted {
private Processing processing;
// Constructors
// Getters/Setters
}
Then call post(new ProcessingRequested(processing)) and post(new ProcessingStarted(processing)) when needed, instead of a single post(processing).

Interface in java

interface A {
public void eg1();
}
interface B {
public void eg1();
}
public class SomeOtherClassName implements A, B {
#Override
public void eg1() {
System.out.println("test.eg1()");
}
}
What is the output and what occurs if method is overriden in interface?
First of all it's of no use to implement both class A and B as both
of them has same method signature i.e both has same method name and
return type.
Secondly you'll need a main method to run the program.
Also in interface you can only declare the methods, the implementation
has to be done in the class which implements it.
interface A {
public void eg1();
}
interface B {
public void eg1();
}
public class Test implements A{
#Override
public void eg1() {
System.out.println("test.eg1()");
}
public static void main (String args[]) {
A a = new test();
a.eg1();
}
}
Output : test.eg1()

How to make out.println() method working in a simple java file without using System Class reference?

public class ABC{ public static void main(String[] args) { out.println("Hello"); } }
This works, though static imports are not generally considered a good thing in java.
import static java.lang.System.out;
public class ABC {
public static void main(String[] args) {
out.println("hello");
}
}

Forcing the use of a specific overload of a method in C#

I have an overloaded generic method used to obtain the value of a property of an object of type PageData. The properties collection is implemented as a Dictionary<string, object>. The method is used to avoid the tedium of checking if the property is not null and has a value.
A common pattern is to bind a collection of PageData to a repeater. Then within the repeater each PageData is the Container.DataItem which is of type object.
I wrote the original extension method against PageData:
public static T GetPropertyValue<T>(this PageData page, string propertyName);
But when data binding, you have to cast the Container.DataItem to PageData:
<%# ((PageData)Container.DataItem).GetPropertyValue("SomeProperty") %>
I got a little itch and wondered if I couldn't overload the method to extend object, place this method in a separate namespace (so as not to pollute everything that inherits object) and only use this namespace in my aspx/ascx files where I know I've databound a collection of PageData. With this, I can then avoid the messy cast in my aspx/ascx e.g.
// The new overload
public static T GetPropertyValue<T>(this object page, string propertyName);
// and the new usage
<%# Container.DataItem.GetPropertyValue("SomeProperty") %>
Inside the object version of GetPropertyValue, I cast the page parameter to PageData
public static T GetPropertyValue<T>(this object page, string propertyName)
{
PageData data = page as PageData;
if (data != null)
{
return data.GetPropertyValue<T>(propertyName);
}
else
{
return default(T);
}
}
and then forward the call onto, what I would expect to be PageData version of GetPropertyValue, however, I'm getting a StackOverflowException as it's just re-calling the object version.
How can I get the compiler to realise that the PageData overload is a better match than the object overload?
The extension method syntax is just syntactic sugar to call static methods on objects. Just call it like you would any other regular static method (casting arguments if necessary).
i.e.,
public static T GetPropertyValue<T>(this object page, string propertyName)
{
PageData data = page as PageData;
if (data != null)
{
//will call the GetPropertyValue<T>(PageData,string) overload
return GetPropertyValue<T>(data, propertyName);
}
else
{
return default(T);
}
}
[edit]
In light of your comment, I wrote a test program to see this behavior. It looks like it does go with the most local method.
using System;
using Test.Nested;
namespace Test
{
namespace Nested
{
public static class Helper
{
public static void Method(this int num)
{
Console.WriteLine("Called method : Test.Nested.Helper.Method(int)");
}
}
}
static class Helper
{
public static void Method(this object obj)
{
Console.WriteLine("Called method : Test.Helper.Method(object)");
}
}
class Program
{
static void Main(string[] args)
{
int x = 0;
x.Method(); //calls the object overload
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
Console.WriteLine();
}
}
}
To make sure the nesting is not affecting anything, tried this also removing the object overload:
using System;
using Test.Nested;
namespace Test
{
namespace Nested
{
public static class Helper
{
public static void Method(this int num)
{
Console.WriteLine("Called method : Test.Nested.Helper.Method(int)");
}
}
}
static class Helper
{
public static void Method(this string str)
{
Console.WriteLine("Called method : Test.Helper.Method(string)");
}
}
class Program
{
static void Main(string[] args)
{
int x = 0;
x.Method(); //calls the int overload
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
Console.WriteLine();
}
}
}
Sure enough, the int overload is called.
So I think it's just that, when using the extension method syntax, the compiler looks within the current namespace first for appropriate methods (the "most local"), then other visible namespaces.
It should already be working fine. I've included a short but complete example below. I suggest you double-check your method signatures and calls, and if you're still having problems, try to come up with a similar short-but-complete program to edit into your question. I suspect you'll find the answer while coming up with the program, but at least if you don't, we should be able to reproduce it and fix it.
using System;
static class Extensions
{
public static void Foo<T>(this string x)
{
Console.WriteLine("Foo<{0}>(string)", typeof(T).Name);
}
public static void Foo<T>(this object x)
{
Console.WriteLine("Foo<{0}>(object)", typeof(T).Name);
string y = (string) x;
y.Foo<T>();
}
}
class Test
{
static void Main()
{
object s = "test";
s.Foo<int>();
}
}