Can I access variables, methods of an Activity Class from a Non-Activity-Class?Are static class variables possible?Stop EditText from gaining focus at Activity startupDoes Python have “private” variables in classes?Android simple login screen crashes app when logging, no errors in eclipse. Which java class activity is at fault?Google map API v2[Android]Object cannot be cast to java.lang.String AsynctaskToolbar title not showing in tabbed activityHow to get selected items in a custom listview to another activity in same custom listviewOSMDROID - loading .map file downloaded from mapsforgeThis use Navigation Drawer, and use Tab, and use Fragment. how communication between FragmentActivity and Fragment?

Anatomically Correct Carnivorous Tree

How can this triangle figure be modeled/drawn with TikZ?

Ex-manager wants to stay in touch, I don't want to

Why was Thor doubtful about his worthiness to Mjolnir?

How to compact two the parabol commands in the following example?

Run script for 10 times until meets the condition, but break the loop if it meets the condition during iteration

Was there ever any real use for a 6800-based Apple I?

Meaning of「〜てみたいと思います」

Why doesn't Rocket Lab use a solid stage?

What's special about a Bunsen burner?

On studying Computer Science vs. Software Engineering to become a proficient coder

List software from restricted, multiverse separately

Word for being out at night during curfew

Why does getw return -1 when trying to read a character?

Is there a spell to protect inanimate objects?

Can I use my laptop, which says 100-240V, in the USA?

Find the cipher used

What does i386 mean on macOS Mojave?

How to slow yourself down (for playing nice with others)

Who was this character from the Tomb of Annihilation adventure before they became a monster?

Is it a bad idea to replace pull-up resistors with hard pull-ups?

Why was the Ancient One so hesitant to teach Dr. Strange the art of sorcery?

What does "Ich wusste, dass aus dir mal was wird" mean?

Drawing lines to nearest point



Can I access variables, methods of an Activity Class from a Non-Activity-Class?


Are static class variables possible?Stop EditText from gaining focus at Activity startupDoes Python have “private” variables in classes?Android simple login screen crashes app when logging, no errors in eclipse. Which java class activity is at fault?Google map API v2[Android]Object cannot be cast to java.lang.String AsynctaskToolbar title not showing in tabbed activityHow to get selected items in a custom listview to another activity in same custom listviewOSMDROID - loading .map file downloaded from mapsforgeThis use Navigation Drawer, and use Tab, and use Fragment. how communication between FragmentActivity and Fragment?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I want to operate some functionality of my activity class from another class. Because my the number of codes in activity class is increasing and difficult to understand later.



This is the activity class



MainActivity.java



public class MainActivity extends AppCompatActivity 

private int number = 7;
public String name = "arafat";

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

printSomething();


private void printSomething()
System.out.println("Hello, World!");




and here is the non-activity class



NonActivityClass.java



public class NonActivityClass 
//can I access the variables and methods from here



How can I will be able to access the global or private fields of activity class from this non-activity class?










share|improve this question
























  • Any variables that it needs to access should be passed in, and should to the greatest degree possible be treated as immutable unless you have very good reason not to. Classes accessing each others variables directly is generally considered a very bad idea. And the fact you're confusing the idea with inheritance shows that you don't really understand OOP yet and should probably spend more effort on that.

    – Gabe Sechan
    Mar 23 at 12:03






  • 1





    You may need to read more about OOP and static keyword.

    – Ibrahim Ali
    Mar 23 at 12:15











  • I have solved this in that way : import android.util.Log; Is the any other way public class NonActivityClass //can I access the variables and methods from here private static final String TAG = "NonActivityClass"; MainActivity mainActivity; public NonActivityClass(MainActivity activity) mainActivity = activity; String name; public void doSomething() name = mainActivity.name; Log.d(TAG, "doSomething: " + name); But are there any other ways for this ?

    – Arafat
    Mar 23 at 19:44


















0















I want to operate some functionality of my activity class from another class. Because my the number of codes in activity class is increasing and difficult to understand later.



This is the activity class



MainActivity.java



public class MainActivity extends AppCompatActivity 

private int number = 7;
public String name = "arafat";

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

printSomething();


private void printSomething()
System.out.println("Hello, World!");




and here is the non-activity class



NonActivityClass.java



public class NonActivityClass 
//can I access the variables and methods from here



How can I will be able to access the global or private fields of activity class from this non-activity class?










share|improve this question
























  • Any variables that it needs to access should be passed in, and should to the greatest degree possible be treated as immutable unless you have very good reason not to. Classes accessing each others variables directly is generally considered a very bad idea. And the fact you're confusing the idea with inheritance shows that you don't really understand OOP yet and should probably spend more effort on that.

    – Gabe Sechan
    Mar 23 at 12:03






  • 1





    You may need to read more about OOP and static keyword.

    – Ibrahim Ali
    Mar 23 at 12:15











  • I have solved this in that way : import android.util.Log; Is the any other way public class NonActivityClass //can I access the variables and methods from here private static final String TAG = "NonActivityClass"; MainActivity mainActivity; public NonActivityClass(MainActivity activity) mainActivity = activity; String name; public void doSomething() name = mainActivity.name; Log.d(TAG, "doSomething: " + name); But are there any other ways for this ?

    – Arafat
    Mar 23 at 19:44














0












0








0


0






I want to operate some functionality of my activity class from another class. Because my the number of codes in activity class is increasing and difficult to understand later.



This is the activity class



MainActivity.java



public class MainActivity extends AppCompatActivity 

private int number = 7;
public String name = "arafat";

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

printSomething();


private void printSomething()
System.out.println("Hello, World!");




and here is the non-activity class



NonActivityClass.java



public class NonActivityClass 
//can I access the variables and methods from here



How can I will be able to access the global or private fields of activity class from this non-activity class?










share|improve this question
















I want to operate some functionality of my activity class from another class. Because my the number of codes in activity class is increasing and difficult to understand later.



This is the activity class



MainActivity.java



public class MainActivity extends AppCompatActivity 

private int number = 7;
public String name = "arafat";

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

printSomething();


private void printSomething()
System.out.println("Hello, World!");




and here is the non-activity class



NonActivityClass.java



public class NonActivityClass 
//can I access the variables and methods from here



How can I will be able to access the global or private fields of activity class from this non-activity class?







java android class android-activity access-modifiers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 2:52







Arafat

















asked Mar 23 at 11:58









ArafatArafat

315




315












  • Any variables that it needs to access should be passed in, and should to the greatest degree possible be treated as immutable unless you have very good reason not to. Classes accessing each others variables directly is generally considered a very bad idea. And the fact you're confusing the idea with inheritance shows that you don't really understand OOP yet and should probably spend more effort on that.

    – Gabe Sechan
    Mar 23 at 12:03






  • 1





    You may need to read more about OOP and static keyword.

    – Ibrahim Ali
    Mar 23 at 12:15











  • I have solved this in that way : import android.util.Log; Is the any other way public class NonActivityClass //can I access the variables and methods from here private static final String TAG = "NonActivityClass"; MainActivity mainActivity; public NonActivityClass(MainActivity activity) mainActivity = activity; String name; public void doSomething() name = mainActivity.name; Log.d(TAG, "doSomething: " + name); But are there any other ways for this ?

    – Arafat
    Mar 23 at 19:44


















  • Any variables that it needs to access should be passed in, and should to the greatest degree possible be treated as immutable unless you have very good reason not to. Classes accessing each others variables directly is generally considered a very bad idea. And the fact you're confusing the idea with inheritance shows that you don't really understand OOP yet and should probably spend more effort on that.

    – Gabe Sechan
    Mar 23 at 12:03






  • 1





    You may need to read more about OOP and static keyword.

    – Ibrahim Ali
    Mar 23 at 12:15











  • I have solved this in that way : import android.util.Log; Is the any other way public class NonActivityClass //can I access the variables and methods from here private static final String TAG = "NonActivityClass"; MainActivity mainActivity; public NonActivityClass(MainActivity activity) mainActivity = activity; String name; public void doSomething() name = mainActivity.name; Log.d(TAG, "doSomething: " + name); But are there any other ways for this ?

    – Arafat
    Mar 23 at 19:44

















Any variables that it needs to access should be passed in, and should to the greatest degree possible be treated as immutable unless you have very good reason not to. Classes accessing each others variables directly is generally considered a very bad idea. And the fact you're confusing the idea with inheritance shows that you don't really understand OOP yet and should probably spend more effort on that.

– Gabe Sechan
Mar 23 at 12:03





Any variables that it needs to access should be passed in, and should to the greatest degree possible be treated as immutable unless you have very good reason not to. Classes accessing each others variables directly is generally considered a very bad idea. And the fact you're confusing the idea with inheritance shows that you don't really understand OOP yet and should probably spend more effort on that.

– Gabe Sechan
Mar 23 at 12:03




1




1





You may need to read more about OOP and static keyword.

– Ibrahim Ali
Mar 23 at 12:15





You may need to read more about OOP and static keyword.

– Ibrahim Ali
Mar 23 at 12:15













I have solved this in that way : import android.util.Log; Is the any other way public class NonActivityClass //can I access the variables and methods from here private static final String TAG = "NonActivityClass"; MainActivity mainActivity; public NonActivityClass(MainActivity activity) mainActivity = activity; String name; public void doSomething() name = mainActivity.name; Log.d(TAG, "doSomething: " + name); But are there any other ways for this ?

– Arafat
Mar 23 at 19:44






I have solved this in that way : import android.util.Log; Is the any other way public class NonActivityClass //can I access the variables and methods from here private static final String TAG = "NonActivityClass"; MainActivity mainActivity; public NonActivityClass(MainActivity activity) mainActivity = activity; String name; public void doSomething() name = mainActivity.name; Log.d(TAG, "doSomething: " + name); But are there any other ways for this ?

– Arafat
Mar 23 at 19:44













1 Answer
1






active

oldest

votes


















0














I have solved this problem,use oop concept and static keyword



I have use one java class and one Activity.I have create Java class object in Activity to call and access class methods and variables like this



This is my code:



public class NonActivityClass 
String Myname = "demo";
static String Myfullname = "";

public static void setName(String name)
Myfullname = name;


public String getName()
Log.e("check_value","working");

public static String getFullName()
return Myfullname;




In this class i have create some static or not static variable and some method like getname(),setname()and getFullname();



MainActivity.java



import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import static com.example.rdprojects.NonActivityClass.Myfullname;
import static com.example.rdprojects.NonActivityClass.getFullName;


public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//create NonActivityClass object
NonActivityClass myclass = new NonActivityClass();

myclass.getName();

//call static variable
Myfullname = "demo demo";
String fullname = getFullName();

Log.e("check_full_name", "" + fullname);







In this activity first of all i have create java class object and use this object to call variable and method . and some static method call directly .
if you try to get data form any java class to in activity try this code.






share|improve this answer























  • I want to access the fields of activity class from non-activity class. You have done a reverse process here.

    – Arafat
    Mar 26 at 15:41











  • Yes we do this make static variables and methods in main activity which you want to access in non-activity class.

    – Android Geek
    Mar 27 at 5:39











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55313496%2fcan-i-access-variables-methods-of-an-activity-class-from-a-non-activity-class%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I have solved this problem,use oop concept and static keyword



I have use one java class and one Activity.I have create Java class object in Activity to call and access class methods and variables like this



This is my code:



public class NonActivityClass 
String Myname = "demo";
static String Myfullname = "";

public static void setName(String name)
Myfullname = name;


public String getName()
Log.e("check_value","working");

public static String getFullName()
return Myfullname;




In this class i have create some static or not static variable and some method like getname(),setname()and getFullname();



MainActivity.java



import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import static com.example.rdprojects.NonActivityClass.Myfullname;
import static com.example.rdprojects.NonActivityClass.getFullName;


public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//create NonActivityClass object
NonActivityClass myclass = new NonActivityClass();

myclass.getName();

//call static variable
Myfullname = "demo demo";
String fullname = getFullName();

Log.e("check_full_name", "" + fullname);







In this activity first of all i have create java class object and use this object to call variable and method . and some static method call directly .
if you try to get data form any java class to in activity try this code.






share|improve this answer























  • I want to access the fields of activity class from non-activity class. You have done a reverse process here.

    – Arafat
    Mar 26 at 15:41











  • Yes we do this make static variables and methods in main activity which you want to access in non-activity class.

    – Android Geek
    Mar 27 at 5:39















0














I have solved this problem,use oop concept and static keyword



I have use one java class and one Activity.I have create Java class object in Activity to call and access class methods and variables like this



This is my code:



public class NonActivityClass 
String Myname = "demo";
static String Myfullname = "";

public static void setName(String name)
Myfullname = name;


public String getName()
Log.e("check_value","working");

public static String getFullName()
return Myfullname;




In this class i have create some static or not static variable and some method like getname(),setname()and getFullname();



MainActivity.java



import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import static com.example.rdprojects.NonActivityClass.Myfullname;
import static com.example.rdprojects.NonActivityClass.getFullName;


public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//create NonActivityClass object
NonActivityClass myclass = new NonActivityClass();

myclass.getName();

//call static variable
Myfullname = "demo demo";
String fullname = getFullName();

Log.e("check_full_name", "" + fullname);







In this activity first of all i have create java class object and use this object to call variable and method . and some static method call directly .
if you try to get data form any java class to in activity try this code.






share|improve this answer























  • I want to access the fields of activity class from non-activity class. You have done a reverse process here.

    – Arafat
    Mar 26 at 15:41











  • Yes we do this make static variables and methods in main activity which you want to access in non-activity class.

    – Android Geek
    Mar 27 at 5:39













0












0








0







I have solved this problem,use oop concept and static keyword



I have use one java class and one Activity.I have create Java class object in Activity to call and access class methods and variables like this



This is my code:



public class NonActivityClass 
String Myname = "demo";
static String Myfullname = "";

public static void setName(String name)
Myfullname = name;


public String getName()
Log.e("check_value","working");

public static String getFullName()
return Myfullname;




In this class i have create some static or not static variable and some method like getname(),setname()and getFullname();



MainActivity.java



import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import static com.example.rdprojects.NonActivityClass.Myfullname;
import static com.example.rdprojects.NonActivityClass.getFullName;


public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//create NonActivityClass object
NonActivityClass myclass = new NonActivityClass();

myclass.getName();

//call static variable
Myfullname = "demo demo";
String fullname = getFullName();

Log.e("check_full_name", "" + fullname);







In this activity first of all i have create java class object and use this object to call variable and method . and some static method call directly .
if you try to get data form any java class to in activity try this code.






share|improve this answer













I have solved this problem,use oop concept and static keyword



I have use one java class and one Activity.I have create Java class object in Activity to call and access class methods and variables like this



This is my code:



public class NonActivityClass 
String Myname = "demo";
static String Myfullname = "";

public static void setName(String name)
Myfullname = name;


public String getName()
Log.e("check_value","working");

public static String getFullName()
return Myfullname;




In this class i have create some static or not static variable and some method like getname(),setname()and getFullname();



MainActivity.java



import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import static com.example.rdprojects.NonActivityClass.Myfullname;
import static com.example.rdprojects.NonActivityClass.getFullName;


public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//create NonActivityClass object
NonActivityClass myclass = new NonActivityClass();

myclass.getName();

//call static variable
Myfullname = "demo demo";
String fullname = getFullName();

Log.e("check_full_name", "" + fullname);







In this activity first of all i have create java class object and use this object to call variable and method . and some static method call directly .
if you try to get data form any java class to in activity try this code.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 25 at 10:09









Android GeekAndroid Geek

5,06421025




5,06421025












  • I want to access the fields of activity class from non-activity class. You have done a reverse process here.

    – Arafat
    Mar 26 at 15:41











  • Yes we do this make static variables and methods in main activity which you want to access in non-activity class.

    – Android Geek
    Mar 27 at 5:39

















  • I want to access the fields of activity class from non-activity class. You have done a reverse process here.

    – Arafat
    Mar 26 at 15:41











  • Yes we do this make static variables and methods in main activity which you want to access in non-activity class.

    – Android Geek
    Mar 27 at 5:39
















I want to access the fields of activity class from non-activity class. You have done a reverse process here.

– Arafat
Mar 26 at 15:41





I want to access the fields of activity class from non-activity class. You have done a reverse process here.

– Arafat
Mar 26 at 15:41













Yes we do this make static variables and methods in main activity which you want to access in non-activity class.

– Android Geek
Mar 27 at 5:39





Yes we do this make static variables and methods in main activity which you want to access in non-activity class.

– Android Geek
Mar 27 at 5:39



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55313496%2fcan-i-access-variables-methods-of-an-activity-class-from-a-non-activity-class%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript