5

I do an import of the full package name / java file, and if I do a <classname>.<method>, SOMETIMES I can get it to access - other times I get a lot of can't use a static in a non static bunch of talk.

I'll admit I'm new to Java, so what do I need to do? Call a class instance first, then call my methods? I'm rather confused by this, as I want to put all of my 'functions' into a FunctionsList.java file, and all of my main Activity (UI) into a MyActivity.java file.

For example:

<MyActivity.java>

import com.example.FunctionsList;

private class MyActivity extends Activity {
  FunctionsList.function();
}

9/10 times I get that static/non-static error.

If I put all of my functions into MyActivity.java, I have zero problems! Anyone help me on what I presume is a basic Java newbie issue?

Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
John
  • 921
  • 1
  • 9
  • 24
  • posting up code can help the community assist you with the issue – Woot4Moo Aug 16 '10 at 19:25
  • 3
    I think you should read up on Object Oriented programming – Falmarri Aug 16 '10 at 19:29
  • I'm coming from a very strong procedural background, and I haven't programming in almost a decade... so I'm learning the way I've always learned best - by diving head first in! – John Aug 16 '10 at 19:33
  • That's all right and good, but you are still missing fundamental Object Orientation concepts. – Santa Aug 16 '10 at 19:45

2 Answers2

16

Here's an example that will hopefully help you out a little.

public class MyFunctionClass {

   public String myFunction() {
      return "This is an instance function.";
   }

   public static String myStaticFunction() {
      return "This is a static function.";
   }

}

Then in your activity you have something like this.

public class MyActivity extends Activity {

   @Override
   public void onCreate() {

      // If you want to call your static function, you do not
      // require an instance of a MyFunctionClass object.
      String myStaticString = MyFunctionClass.myStaticFunction();

      // If you want to call your instance function, then you need
      // to create a MyFunctionClass first.
      MyFunctionClass variableName = new MyFunctionClass();
      String myInstanceString = variableName.myFunction();
   }
}

As Jon mentioned, you'll probably save yourself some frustration if you read up on object-oriented programming before diving in. There are some basic things that a new programmer will need to understand before diving in. Good luck!

bporter
  • 4,467
  • 2
  • 19
  • 29
  • Exactly!! That is 100% exactly what I needed! That's what was confusing me, as I didn't understand why the heck I might have to create the MyFunctionClass, first. So I should be able to call MyFunctionClass as a global in my Activity.java, and then pass any of my public functions via (using your notation) variableName.myFunction. Thank you, class structure makes a lot more sense to me now! – John Aug 16 '10 at 19:48
  • Glad to be of help. A word of caution with a class full of static functions however (should you decide to do that in the future): Be sure to consider adding the synchronized modifier to the function declaration. If you are calling static functions from different threads simultaneously, you could potentially run into issues. Again, good luck with your project. http://www.janeg.ca/scjp/threads/synchronized.html – bporter Aug 16 '10 at 19:53
  • I usually do a version of that using a boolean isRunning, if my UI is going to access something at a user-determined speed, but this looks like a more efficient method of doing so! – John Aug 17 '10 at 16:36
3

If you want to use a non-static method, you have to have an instance of the class to call the method on. If you want to use a static method, you don't need an instance.

As an example, suppose you tried to call String.length() - what could that return? It's trying to find the length of something, but you haven't specified which string you're interested in. The same is true for other instance methods - the results will usually depend on which object you're calling them on, which is why they're instance methods to start with.

As an aside, I would strongly recommend you to learn the basics of Java first, before trying to use Android. That way when you get into genuinely tricky problems, you won't have to wonder whether it's part of Android or whether it's a simple Java error. See my answer on a related question for more advice about this.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I understand where you're coming from - but I feel like I've hit most of the basics. I've already written 2 complete Android applications that do exactly what I expect of them - but all the functions and such are contained in one Activity.java file, and it simply makes it difficult for me to follow. I have a strong feeling the problem is simply my lack of experience in full OOP mode, so I'm needing to learn a bit more of how OOP operates. – John Aug 16 '10 at 19:36
  • 1
    @John: Well, I'd call this one of the most fundamental things to understand in Java, so my advice remains. I suggest you explore OOP in a console app too... it's just so much easier to do there than in a phone emulator. – Jon Skeet Aug 16 '10 at 19:41
  • @John Also, note that Java, as a language, almost forces you to structure your code in an Object-Oriented manner. You can try to fight it, but it'll just make life difficult for you. Unlike C++ or Python, Java is very limited in supporting multiple programming paradigms (for better or worse). – Santa Aug 16 '10 at 19:47
  • Just started playing with Python (loving it by the way) - so I'm working on increasing my skill with OOP. Jon, I started out working in console mode, but I'm finding I have an easier time picking out what is going on directly on my phone / using DDMS on Eclipse. Thanks, though! – John Aug 17 '10 at 16:37