Java return Object [closed] - java

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 3 years ago.
Improve this question
I don’t know how to return the value of the string here, in short, I need the output of this string that is stored in the function:
public Object Directory(String directory) {
String directory1 = "directory";
return directory1;
}
public static void main(String... args) throws Exception {
System.out.println(directory1);
}
This is my interface in another file:
public interface Interface extends Remote {
Object FileName(String filename) throws RemoteException;
Object Directory(String directory) throws RemoteException;
}

If I understand you. You can try so.
public static String Directory(String directory) {
String directory1 = "directory";
return directory1;
}
public static void main(String... args) throws Exception {
System.out.println(Directory("directory1"));
}

In Java you must declare the return type of a function. In your case you have defined the return type of Object when you really want a return type of String

Related

Error Modding minecraft 1.12.2 please guys [closed]

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 8 days ago.
Improve this question
Item Creation Error (public class ItemsRegistry ) The nested type ItemsRegistry cannot hide an enclosing type.
package com.thedoffee.sd.registers;
public class ItemsRegistry {
#ObjectHolder("tut")
#Mod.EventBusSubscriber
public class ItemsRegistry {
#ObjectHolder("key")
public static final Item KEY = null;
#SubscribeEvent
public static void onRegistryItem(RegistryEvent.Register<Item> e) {
e.getRegistry().register(new ItemKey());
}
#SubscribeEvent
#SideOnly(Side.CLIENT)
public static void onRegistryModel(ModelRegistryEvent e) {
//
}
}
}

Write a constructor in class Doctor to initialize the private field doctor_name [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
Write a constructor in class Doctor to initialize the private field doctor_name. How can I write this?
class Doctor{
public Doctor(private String doc_name){
}
}
Here is an example class Doctor with a constructor.
class Doctor{
private String doc_name;
Doctor(String doc_name){
this.doc_name = doc_name;
}
}
You can also add a setter and getter to apply encapsulation.
class Doctor{
private String doc_name;
Doctor(String doc_name) {
this.doc_name = doc_name;
}
public void setDoctorName(String doc_name) {
this.doc_name = doc_name;
}
public String getDoctorName() {
return this.doc_name;
}
}
I've made a simple implementation of the class Doctor.
class Main {
public static void main(String[] args) {
Doctor doctor1 = new Doctor("Someone");
System.out.println(doctor1.getDoctorName());
doctor1.setDoctorName("Someone Else");
System.out.println(doctor1.getDoctorName());
}
}
Output:
Someone
Someone Else

Instantiating two instances of a class [closed]

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 have a quick question
Suppose I were to create a data definition class named Campaign
and then I want to make an implementation class
Let's say in this implementation class I have a few variables(campaignName, campaignType, campaignGoal) and some methods (getCampName, getCampType, getCampGoal).
I want to create two different campaigns classes, and then compare their information at the end of the program with a print method.
Would this be a proper way of declaring information for the first campaign:
Campaign campaign1 = new Campaign();
which is in the main method, and then let's say I want to get the name of the first campaign, which is just hardcoded for now
public static String campaign1.getCampName(){
campaign1.setCampName("IT Student Scholarship Fund");
}
I just wanted to know how to do this. Thank you.
getCampName() should look something like:
public String getCampName() { return this.campaignName; }
a then simply
campaign1.getName();
You should stop the practice of putting all of your code in a main method. Instead, instantiate your Campaign instances and call methods on each one using a driver method in your primary class. In addition, you can override the equals() method to do the comparison (or implement Comparator).
public class CampaignTest{
public void runTest(){
Campaign c1 = new Campaign("First Campaign");
Campaign c2 = new Campaign("Second Campaign");
Campaign c11 = new Campaign("First Campaign");
System.out.println("c1==c2: " + c1.equals(c2));
System.out.println("c2==c11: " + c2.equals(c11));
System.out.println("c1==c11: " + c1.equals(c11));
}
public static void main(String... args){
CampaignTest test = new CampaignTest();
test.runTest();
}
private class Campaign{
private String name;
public Campaign(String n){
this.name = n;
}
public String getName(){
return name;
}
#Override
public boolean equals(Object other){
if(other instanceof Campaign && ((Campaign)other).getName().equals(name)){
return true;
}
return false;
}
}
}

Unexpected NullPointerException [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How can NullReferenceException happen here:
public class DataContextBinder extends Binder {
private static final String DATA_CONTEXT = "DataContext";
// Fields
private final Listener<PropertyChangedEventArgs<Object>> dataContextListener = new Listener<PropertyChangedEventArgs<Object>>() {
#Override
public void onEvent(PropertyChangedEventArgs<Object> args) {
setSource(args.getNewValue());
}
};
// Constructors
protected DataContextBinder(Object target, String targetPropertyName,
Binding binding) {
super(Property.getPropertyValue(target, DATA_CONTEXT), target,
targetPropertyName, binding);
}
// Methods
protected void createListeners(Object source) {
Log.e("DataContextBinder", "DCL = " + dataContextListener.toString());
// It throws NullPointerException on dataContextListener.toString()
Property.addPropertyChangedListener(getTarget(), DATA_CONTEXT,
dataContextListener);
super.createListeners(source);
}
protected void removeListeners(Object source) {
Property.removePropertyChangedListener(getTarget(), DATA_CONTEXT,
dataContextListener);
super.removeListeners(source);
}
}
dataContextListener is null..

How do I write the class that contains the main function for this code? [closed]

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 9 years ago.
Improve this question
public class Soldier
{
private String soldierName;
public void setSoldierName( String name )
{
SoldierName = name;
}
public String getSoldierName()
{
return soldierName;
}
public void displayMessage()
{
System.out.printf( "The name of \n%s!\n",
getSoldierName() );
}
}
If you just want the class to have a main method, just add it as you would to any other class:
public class Soldier
{
private String soldierName;
public void setSoldierName( String name )
{
SoldierName = name;
}
public String getSoldierName()
{
return soldierName;
}
public void displayMessage()
{
System.out.printf( "The name of \n%s!\n",
getSoldierName() );
}
public static void main(String args[]) {
}
}
To actually do something useful in the main, you might want to create an instance of the class in there, set the soldier name and then call the displayMessage() method, for example. So like this:
public static void main(String args[]) {
Soldier soldier = new Soldier();
soldier.setSoldierName("John");
soldier.displayMessage();
}

Categories